Mega Code Archive

 
Categories / Java Book / 003 Essential Classes
 

0220 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(); } }