Topics Index

More Utility Classes > Scanner >
Siva Nookala - 20 Feb 2017
Scanner reads tokens from the underlying source which could be a File or InputStream or a String. The tokens are usually separated by spaces or we can also use custom delimiters like commas(,), semi-colon(;) etc., by using useDelimiter(","), useDelimiter(";") etc.,

If we don't want to read all the tokens, but only want certain type of tokens we can do that. Scanner provides various default matching types for primitive types like int, double etc.,. The methods are hasNext, hasNextInt, hasNextDecimal, hasNextFloat etc., Scanner also provides the matching ability using the regular expressions.

The following steps are required in every Scanning operation:
  • Determine if a specific type of input is available by calling one of Scanner's hasNextXXXX methods.
  • If input is available, read it by calling one of Scanner's nextX methods.
  • Repeat the process until input is exhausted.
  • Close the Scanner by calling close().
The complete list of hasNextXXX methods are
boolean hasNext()
boolean hasNext(Pattern pattern)
boolean hasNext(String pattern)
boolean hasNextBigDecimal()
boolean hasNextBigInteger()
boolean hasNextBigInteger(int radix)
boolean hasNextBoolean()
boolean hasNextByte()
boolean hasNextByte(int radix)
boolean hasNextDouble()
boolean hasNextFloat()
boolean hasNextInt()
boolean hasNextInt(int radix)
boolean hasNextLine()
boolean hasNextLong()
boolean hasNextLong(int radix)
boolean hasNextShort()
boolean hasNextShort(int radix)

The complete list of next methods are
String next()
String next(Pattern pattern)
String next(String pattern)
BigDecimal nextBigDecimal()
BigInteger nextBigInteger()
BigInteger nextBigInteger(int radix)
boolean nextBoolean()
byte nextByte()
byte nextByte(int radix)
double nextDouble()
float nextFloat()
int nextInt()
int nextInt(int radix)
String nextLine()
long nextLong()
long nextLong(int radix)
short nextShort()
short nextShort(int radix)
Lets look at an example which reads the marks of various students and finds the average mark for the class.
Average Scanner Demo
import java.util.*;

class AverageScannerDemo
    public static void main(String arg[])
            FileWriter fout = new FileWriter("student-marks.txt");
            fout.write("25 34 21 45 27 18 49 43");
            FileReader reader = new FileReader("student-marks.txt");
            Scanner scanner = new Scanner(reader);
            double sum = 0.0;
            int count = 0;
                sum += scanner.nextInt();
            System.out.println("Sum : " + sum);
            System.out.println("Tokens Count : " + count);
            System.out.println("Average : " + (sum/count));
        catch(Exception e)
            System.out.println("Exception occured : " + e.getMessage());

Sum : 262.0
Tokens Count : 8
Average : 32.75


We prepared the sample file by writing the marks into the student-marks.txt file. After that we created a Scanner using the FileReader for the student-marks.txt. We are only looking for integers in the file using hasNextInt and reading them using nextInt method. We are summing up those integer tokens and finding the average.

  • Change the input to add additional tokens like Alan, true, 34.5, A in between those integer tokens and see that they are ignored, since we are only looking for integers.
  • After adding additional invalid tokens, change the methods hasNextInt/nextInt to hasNext/next and see what exception does it throw.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App