Interviews - C# Corner

Vijay Kumari
What is Scanner class used for ? When was it introduced in Java ?
By Vijay Kumari in Java onSep 17 2019
  • VENKATESWARULU S
    Oct, 2019 16

    Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
    To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
    To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
    To read strings, we use nextLine().
    To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.

    Example
    // Java program to read some values using Scanner
    // class and print their mean.
    import java.util.Scanner;

    public class ScannerDemo2
    {
    public static void main(String[] args)
    {
    // Declare an object and initialize with
    // predefined standard input object
    Scanner sc = new Scanner(System.in);

    1. // Initialize sum and count of input elements
    2. int sum = 0, count = 0;
    3. // Check if an int value is available
    4. while (sc.hasNextInt())
    5. {
    6. // Read an int value
    7. int num = sc.nextInt();
    8. sum += num;
    9. count++;
    10. }
    11. int mean = sum / count;
    12. System.out.println("Mean: " + mean);
    13. }

    }

    Input
    101
    223
    238
    892
    99
    500
    728

    OUTPUT
    Mean: 397

    Example 2
    // Java program to read data of various types using Scanner class.
    import java.util.Scanner;
    public class ScannerDemo1
    {
    public static void main(String[] args)
    {
    // Declare the object and initialize with
    // predefined standard input object
    Scanner sc = new Scanner(System.in);

    1. // String input
    2. String name = sc.nextLine();
    3. // Character input
    4. char gender = sc.next().charAt(0);
    5. // Numerical data input
    6. // byte, short and float can be read
    7. // using similar-named functions.
    8. int age = sc.nextInt();
    9. long mobileNo = sc.nextLong();
    10. double cgpa = sc.nextDouble();
    11. // Print the values to check if the input was correctly obtained.
    12. System.out.println("Name: "+name);
    13. System.out.println("Gender: "+gender);
    14. System.out.println("Age: "+age);
    15. System.out.println("Mobile Number: "+mobileNo);
    16. System.out.println("CGPA: "+cgpa);
    17. }

    }

    INPUT

    Geek
    F
    40
    9876543210
    9.9

    OUPUT
    Name: Geek
    Gender: F
    Age: 40
    Mobile Number: 9876543210
    CGPA: 9.9

    Sometimes, we have to check if the next value we read is of a certain type or if the input has ended (EOF marker encountered).
    Then, we check if the scanner’s input is of the type we want with the help of hasNextXYZ() functions where XYZ is the type we are interested in. The function returns true if the scanner has a token of that type, otherwise false. For example, in the below code, we have used hasNextInt(). To check for a string, we use hasNextLine(). Similarly, to check for a single character, we use hasNext().charAt(0).

    • 0
  • Rohit Gupta
    Sep, 2019 17

    Uses of Java Scanner

    • Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default.
    • It is the easiest way to read input in Java program,
    • By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.
    • The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

    It was introduced in Java 7

    For a detailed tutorial on Java Scanner please visit https://www.c-sharpcorner.com/article/java-scanner-tutorials/

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS