Scanner Class in Java

Scanner Class is easy to understand and implement. That is why it is best suited for beginners. It is not very efficient because it fails the time constraint and hence not at all advisable for competitive programming.

Scanner Class in Java

‘Scanner’ is a class of java.util.Scanner package. Define an object of Scanner Class and assign it to an object reference and pass the predefined object System.in as its argument which gives it access to the input data stream. Read:-

  • A string using next().
  • A string until a newline appears using nextLine().
  • A character using next().charAt(0).
  • Numerical values using nextInt() and nextDouble() and nextFloat() for integer, double and float data types respectively. Likewise, for other data types.

You may also read,

Take inputs from the user using scanner class in Java

import java.util.Scanner;
public class inputdemo {

  public static void main(String[] args) {
    
     
        Scanner sc = new Scanner(System.in); 	// defining object of Scanner class with passing 'System.in' as the argument
   
        String str = sc.nextLine(); 			// string input
        
        char ch  = sc.next().charAt(0); 		// character input
  
        int num1 = sc.nextInt();  			// integer input
        double num2 = sc.nextDouble();			// double input
        float num3 = sc.nextFloat();			// float input
        
        sc.close(); // the object reference can be closed once its purpose has been served // though this is not mandatory
  
        // Checking the values of input taken. 
        System.out.println(str); 
        System.out.println(ch); 
        System.out.println(num1); 
        System.out.println(num2); 
        System.out.println(num3); 
  }

}

Input

The Weeknd
yes
12345
1221.324
24234324.43254324

Output

The Weeknd
y
12345
1221.324
2.4234324E7

 

The user may have to clear the buffer in some conditions for instance in the following code.

import java.util.Scanner;

public class inputdemo0 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();     // integer input

        double d=scan.nextDouble(); // double input

        String s=scan.nextLine();   // string input

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

Input:

123
123.456
hello

Output:

String: 
Double: 123.456
Int: 123

The string input appears to be blank because it contains a newline. Debug this by writing scan.nextLine() in the code.

import java.util.Scanner;

public class inputdemo0 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();     // integer input

        double d=scan.nextDouble(); // double input

        scan.nextLine();            // clearing the buffer

        String s=scan.nextLine();   // string input

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

Input:

123
123.456
hello

Output:

String: hello
Double: 123.456
Int: 123

Footnote:

For self-help on Scanner Class, refer the below link.

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

You may also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *