Java | Scanner Class

05 min read

Scanner class:

Scanner class is defined in the java.util package.To write a program using 'Scanner' class, import java_util package as follows:

importjava.util.*; //This imports all the classes defined in 'util' package.
(or)
importjava.util.Scanner; //imports only the Scanner class in the 'util' package.

 

If more than 1 class of a package has to be used, it is better to go for the first way of importing the classes. However, there is no need to write an import statement as 'import java.lang.*;', as this package is imported by default.

Scanner class is used to read formatted input from the console, a string, a file or any source that implements the 'Readable' interface or 'ReadableByteChannel'. To make use of this Scanner class, to read user input, first an object of this class must be created. In Java, all objects are created dynamically (at runtime), using the new operator.

Eg: new Scanner(System.in); //Creates an object for Scanner class & initializes it by invoking the Scanner(...) constructor.

In the sub-expression above, 'new' operator instantiates the Scanner class (ie., creates an object for the Scanner class); it also invokes the constructor of the class; after creating the object (ie., allocating space for the object in the memory) it returns a reference to the object (address), it creates. This reference (address) should be stored in an appropriate variable. To store the reference of the 'Scanner' object, the reference variable, should be of the same type, 'Scanner'.

Scanner sc; //creates a reference variable 'sc' of type 'Scanner' 
sc = new Scanner(System.in); //creates an object for 'Scanner' & stores its reference in the reference
variable, 'sc'

The above 2 statements can be combined into a single line as follows. Scanner sc = new Scanner(System.in)

 

This reference variable can be used to invoke the methods of the Scanner class, for reading the input. There are several types of constructors defined in Scanner class. The most important of these are as follows:

  • Scanner(File f)  //This constructor creates an object of Scanner class that uses the file reference variable 'f', as the source of input.
  • Scanner(String s) //This constructor creates an object of Scanner class that uses the string 's' as the source of input.

Example: String s = "Anu 9.3 Ajay 8.9";

Scanner Sc = new Scanner(s); //When appropriate methods are in\ oked for reading input, it will be read from the string.

  • Scanner(InputStrearn is) //This constructor creates an object of Scanner class that uses the specified input stream as the source of input.

Example: Scanner sc = new Scanner(System.in);

The standard input device, which is the keyboard by default, is provided as the source of input. 

 

 

 

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error