• Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.


  • Breaking Input into Tokens By default, a scanner uses white space to separate tokens. (White space characters include blanks, tabs, and line terminators.)


  • To see how scanning works, let's look at ScanXan, a program that reads the individual words in xanadu.txt and prints them out, one per line.


  •  
    import java.io.*;
    import java.util.Scanner;
    
    public class ScanXan {
        public static void main(String[] args) throws IOException {
    
            Scanner s = null;
    
            try {
                s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
    
                while (s.hasNext()) {
                    System.out.println(s.next());
                }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        }
    }
    
    
  • Notice that ScanXan invokes Scanner's close method when it is done with the scanner object. Even though a scanner is not a stream, you need to close it to indicate that you're done with its underlying stream.


  • The output of ScanXan looks like this:


  •  
    In
    Xanadu
    did
    Kubla
    Khan
    A
    stately
    pleasure-dome
    ...
    
    
  • To use a different token separator, invoke useDelimiter(), specifying a regular expression. For example, suppose you wanted the token separator to be a comma, optionally followed by white space. You would invoke,


  •  
    s.useDelimiter(",\\s*");
    
    
  • Translating Individual Tokens The ScanXan example treats all input tokens as simple String values. Scanner also supports tokens for all of the Java language's primitive types (except for char), as well as BigInteger and BigDecimal.


  • The ScanSum example reads a list of double values and adds them up. Here's the source:


  •  
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Locale;
    
    public class ScanSum {
        public static void main(String[] args) throws IOException {
    
            Scanner s = null;
            double sum = 0;
    
            try {
                s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt")));
                s.useLocale(Locale.US);
    
                while (s.hasNext()) {
                    if (s.hasNextDouble()) {
                        sum += s.nextDouble();
                    } else {
                        s.next();
                    }   
                }
            } finally {
                s.close();
            }
    
            System.out.println(sum);
        }
    }
    
    
  • And here's the sample input file, usnumbers.txt


  •  
    8.5
    32,767
    3.14159
    1,000,000.1
    
    
  • The output string is "1032778.74159". The period will be a different character in some locales, because System.out is a PrintStream object, and that class doesn't provide a way to override the default locale