Parsing Input using Scanner

A Scanner object can be used to read text input from a number of sources. The input source can be a file, an input stream or even from a string. It internally uses the Regular Expression Api for parsing and getting the input. Let us see a simple example to parse a String object,

also read:

Scanner scanner = new Scanner('Have a nice day');
while (scanner.hasNext()){

In the above example, we have used the java.util.Scanner class for parsing the input from the String ‘Have a nice day’. Note that the default delimiter that the Scanner will use during parsing is white-space. If you run the above program, you will see the following output in your console,


Now, let us see how to override the default delimiter during the scanning process using the following code,

Scanner scanner = new Scanner('Once upon a time, there lived a king');
while (scanner.hasNext()){

In the above code snippet, we have explicitly specified comma as the delimiter by calling the Scanner.useDelimter() method before the scanning process. The output in this case is,

Once upon a time
 there lived a king

The above examples assumed that the input will be always a text, however that wont be the case always, because of which we have method variants in the Scanner class that will operate on different data-types like integer, float, Boolean, byte, float etc. Consider the following code snippet which will illustrate that,

Scanner scanner = new Scanner('1 3 5 7 9 11 13');
while (scanner.hasNextInt()){

Note the use of Scanner.hasNextInt() and Scanner.nextInt() for parsing input with integer content. Similarly we have hasNextLong()/nextLong(), hasNextBoolean()/nextBoolean(), hasNextByte()/nextByte() for long, boolean and byte data-types respectively. It is also possible to parse a mixture of different data-types. For example, consider the following code snippet,

Scanner scanner = new Scanner('Hello 1 3.6 123456789000 true');
System.out.println(scanner.hasNext() == true ? : '');
System.out.println(scanner.hasNextInt() == true ? scanner.nextInt() : '');
System.out.println(scanner.hasNextDouble() == true ? scanner.nextDouble() : '');
System.out.println(scanner.hasNextLong() == true ? scanner.nextLong() : '');
System.out.println(scanner.hasNextBoolean() == true ? scanner.nextBoolean() : '');

The output of the above code is,


It is always advisable to check for the existence of the value with the correct data-type by calling the Scanner.hasNextXXX() methods before trying to get the value using Scanner.nextXXX() methods. If any mismatch is found, then InputMismatchException will be thrown at the run-time.

Scanner class has a close() method which should be called after being done with the various operations like this,


It is illegal to call any of the operations after closing the Scanner object and such calls will result in throwing IllegalStateException. As mentioned earlier, the input source for a Scanner object can originate not only from a String but also from a File or from an Input Stream. So, the following statements are valid too.

Scanner scanner = new Scanner(
    new FileInputStream('someFile.txt'));

Scanner scanner = new Scanner(
    new File('anotherFile.txt'));

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!