Scanner read and scan

In this chapter you will learn:

  1. How to use Scanner
  2. Check to see if a certain type of data is available
  3. Scanner reads the input by one of Scanner's nextX methods.
  4. Demo code for how to use the hasNext and read next methods
  5. How to use Scanner to check if there is another line
  6. How to bypass a pattern

Scanner checker and reader

In general, to use Scanner, follow this procedure:

  • Determine if a specific type of input is available by calling one of Scanner's hasNextX methods.
  • If available, read it by calling one of Scanner's nextX methods.

Scanner defines two sets of methods that enable you to read input. The first are the hasNextX methods. These methods determine if the specified type of input is available.

hasNext methods

The following methods are the hasNext methods. From the name we should know which one to use in order to see if a certain type of data is available next.

  • 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)

nextX methods

Scanner reads the input by one of Scanner's nextX methods.

  • 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)

A demo

The following code uses the hasX method to determine the data type then choose what variable to assign the value:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*from   j  a  va 2  s. c  o  m*/
public class MainClass {
  public static void main(String args[]) throws IOException {

    int i;
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
      if (src.hasNextInt()) {
        i = src.nextInt();
        System.out.println("int: " + i);
      } else if (src.hasNextDouble()) {
        d = src.nextDouble();
        System.out.println("double: " + d);
      } else if (src.hasNextBoolean()) {
        b = src.nextBoolean();
        System.out.println("boolean: " + b);
      } else {
        str = src.next();
        System.out.println("String: " + str);
      }
    }

    fin.close();
  }
}

The code above generates the following result.

If has next line

The following code shows how to use the hasNextLine().

import java.io.File;
import java.util.Scanner;
/*from  j a v  a 2s.com*/
public class Main {
  public static void main(String[] args) throws Exception{
    File file = new File("data.txt");

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      System.out.println(line);
    }
  }
}

Bypass a pattern

bypass a pattern using skip( ):

Scanner skip(Pattern pattern) 
Scanner skip(String pattern)

If pattern is matched, skip( ) simply advances beyond it and returns a reference to the invoking object. If pattern is not found, skip( ) throws NoSuchElementException.

Next chapter...

What you will learn in the next chapter:

  1. How to set Delimiters
Home » Java Tutorial » I/O
RandomAccessFile
FilenameFilter
StreamTokenizer
Console
Console password reading
Scanner creation
Scanner read and scan
Scanner Delimiters
Scanner finding