USING SCANNER FOR INPUT IN JAVA

1–2 minutes

read

USING SCANNER FOR INPUT IN JAVA

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings.

In Java, we input with the help of the Scanner class. Java has a number of predefined classes which we can use.

import java.util.Scanner;  // This will import just the Scanner class
import java.util.*;  // This will import the entire java.util package

After importing, we need to write the following statement in our program.

Scanner s = new Scanner (System.in);

Here by writing Scanner s, we are declaring s as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.

Now, how to take input value,

int n;
n = s.nextInt();      //   s is object of Scanner class

Screen Shot 2017-10-02 at 7.14.18 PM.png

 

Screen Shot 2017-10-02 at 7.14.55 PM.png

Leave a comment