Scanner

Scanner is the complement of Formatter and reads formatted input. Scanner can be used to read input from the console, a file, a string, or any source.

The Scanner Constructors

Scanner(File from) throws FileNotFoundException
Creates a Scanner that uses the file specified by from as a source for input.
Scanner(File from, String charset) throws FileNotFoundException
Creates a Scanner that uses the file specified by from with the encoding specified by charset as a source for input.
Scanner(InputStream from)
Creates a Scanner that uses the stream specified by from as a source for input.
Scanner(InputStream from, String charset)
Creates a Scanner that uses the stream specified by from with the encoding specified by charset as a source for input.
Scanner(Readable from)
Creates a Scanner that uses the Readable object specified by from as a source for input.
Scanner (ReadableByteChannel from)
Creates a Scanner that uses the ReadableByteChannel specified by from as a source for input.
Scanner(ReadableByteChannel from, String charset)
Creates a Scanner that uses the ReadableByteChannel specified by from with the encoding specified by charset as a source for input.
Scanner(String from)
Creates a Scanner that uses the string specified by from as a source for input.

The following code creates a Scanner that reads the file Test.txt:


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

FileReader implements the Readable interface. Thus, the call to the constructor resolves to Scanner(Readable).

This next line creates a Scanner that reads from standard input, which is the keyboard by default:


Scanner conin = new Scanner(System.in);

System.in is an object of type InputStream. Thus, the call to the constructor maps to Scanner(InputStream).

The next sequence creates a Scanner that reads from a string.


String instr = "1 1.23 this is a test."; 
Scanner conin = new Scanner(instr);

Scanning Basics

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.

boolean hasNext( )
Returns true if another token of any type is available to be read.
boolean hasNext(Pattern pattern)
Returns true if a token that matches the pattern passed in pattern is available to be read.
boolean hasNext(String pattern)
Returns true if a token that matches the pattern passed in pattern is available to be read.
boolean hasNextBigDecimal( )
Returns true if the next is a BigDecimal object.
boolean hasNextBigInteger( )
Returns true if the next is a BigInteger object. Returns false otherwise. The default radix is used. Unless changed, the default radix is 10.
boolean hasNextBigInteger(int radix)
Returns true if the next is a BigInteger object in specified radix.
boolean hasNextBoolean( )
Returns true if the next is a boolean value.
boolean hasNextByte( )
Returns true if the next is a byte value. Returns false otherwise. The default radix is used. Unless changed, the default radix is 10.
boolean hasNextByte(int radix)
Returns true if the next is a byte value in the specified radix.
boolean hasNextDouble( )
Returns true if the next is a double value.
boolean hasNextFloat( )
Returns true if the next is a float value.
boolean hasNextInt( )
Returns true if the next is an int value. Returns false otherwise. The default radix is used. Unless changed, the default radix is 10.
boolean hasNextInt(int radix)
Returns true if the next is an int value in the specified radix.
boolean hasNextLine( )
Returns true if a line of input is available.
boolean hasNextLong( )
Returns true if the next is a long value. Returns false otherwise. The default radix is used. Unless changed, the default radix is 10.
boolean hasNextLong(int radix)
Returns true if the next is a long value in the specified radix.
boolean hasNextShort( )
Returns true if the next is a short value. Returns false otherwise. The default radix is used. Unless changed, the default radix is 10.
boolean hasNextShort(int radix)
Returns true if the next is a short value in the specified radix.

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

String next( )
Returns the next token of any type from the input source.
String next(Pattern pattern)
Returns the next token that matches the pattern passed in pattern from the input source.
String next(String pattern)
Returns the next token that matches the pattern passed in pattern from the input source.
BigDecimal nextBigDecimal( )
Returns the next BigDecimal object.
BigInteger nextBigInteger( )
Returns the next BigInteger object. The default radix is used. The default radix is 10.
BigInteger nextBigInteger(int radix)
Returns the next BigInteger object using the specified radix.
boolean nextBoolean( )
Returns the next boolean value.
byte nextByte( )
Returns the next byte value. The default radix is used. The default radix is 10.
byte nextByte(int radix)
Returns the next byte value using the specified radix.
double nextDouble( )
Returns the next double value.
float nextFloat( )
Returns the next float value.
int nextInt( )
Returns the next int value. The default radix is used. The default radix is 10.
int nextInt(int radix)
Returns the next int value using the specified radix.
String nextLine( )
Returns the next line of input as a string.
long nextLong( )
Returns the next long value. The default radix is used. The default radix is 10.
long nextLong(int radix)
Returns the next long value using the specified radix.
short nextShort( )
Returns the next token as a short value. The default radix is used. The default radix is 10.
short nextShort(int radix)
Returns the next short value using the specified radix.

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


/**
 *Output:
String: Testing
String: Scanner
int: 10
double: 12.2
String: one
boolean: true
String: two
boolean: false
 */

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

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();
  }
}
Home 
  Java Book 
    Essential Classes  

Scanner:
  1. Scanner
  2. Setting Delimiters
  3. findInLine( )
  4. findWithinHorizon( )
  5. skip( )
  6. new Scanner(FileReader file)
  7. new Scanner(InputStream source)
  8. Scanner: hasNext()
  9. Scanner: hasNextBoolean()
  10. Scanner: hasNextDouble()
  11. Scanner: hasNextInt()
  12. Scanner: hasNextLine()
  13. Scanner: next()
  14. Scanner: nextBoolean()
  15. Scanner: nextDouble()
  16. Scanner: nextInt()
  17. Scanner: nextLine()