Menu
Topics Index
...
`


More Utility Classes > Scanner >
Siva Nookala - 20 Feb 2017
The Scanner class is a class in java.util package, which allows a user to read values of various types.
The Scanner looks for tokens in the input. A token is a series of characters that ends with whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file.
For example, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token.

Constructors
Scanner class has several constructors. Among them, the following are particularly useful.
Constructor Description
Scanner(File source) Constructs a new scanner that produces values scanned from the specified file. If the source file is not found, a FileNotFoundException is thrown. Since it is a cheked exception, it must be caught or forwarded by putting the phrase “throws FileNotFoundException” in the method header.
Scanner(InputStream source) Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(String source) Constructs a new scanner that produces values scanned from the specified string.
Let's now see different ways of instantiating a Scanner object.
Instantiating a Scanner object by passing a File object as parameter
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class ScannerConstructorDemo1
{
    public static void main(String arg[])
    {
        // Read from a file rather than the keyboard
        Scanner sc = new Scanner(new File("FIFA14.txt"));    // LINE A
        
        // Check if sc has another token in the file
        while(sc.hasNext())
            System.out.println(sc.next());
        
        // Close the scanner
        sc.close();    
    }
}
OUTPUT

Germany
Argentina
Netherlands
Brazil
Colombia
France
Costa-Rica
Belgium

DESCRIPTION

Here, we have instantiated a Scanner object by passing a file (FIFA14.txt)(LINE A). (Assume that FIFA14.txt exists.) hasNext() returns true if the input file has another token. SOP statement prints out each token. Finally, close() closes the scanner.

  • To read from keyboard rather than from a file, we instantiate a Scanner object with System.in.
NOTE: System.in is an InputStream.
Scanner in = new Scanner(System.in);   // Reading from the keyboard


Let's now instantiate a Scanner object by passing a String as parameter.
Instantiating a Scanner object by passing a String as parameter
import java.util.Scanner;

class ScannerConstructorDemo2
{
    public static void main(String arg[])
    {
        String str = "Germany are the champions";
                
        Scanner sc = new Scanner(str);       // LINE A
        
        // Check if sc has another token in the string
        while(sc.hasNext())
            System.out.println(sc.next());
        
        // Close the scanner
        sc.close();    
    }
}
OUTPUT

Germany
are
the
champions

DESCRIPTION

Here, we have instantiated a Scanner object using a string (LINE A). Then, hasNext() checks to see if scanner has another token in the string. If there is a token, it is displayed.

THINGS TO TRY
  • Create a file named data.txt and store some integers in it (e.g., 1 to 10). Then instantiate a Scanner object by passing this file as a parameter. Try to Use hasNextInt() method and find the sum of all integers stored in the file.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App