// File: EOFTest.java // The outer loop shows how to use Scanner to process any number of data // values - in this case, lines of text - until a "sentinel value" is read. // For each line read, we create a second Scanner object associated with that // line only. Then, in the inner loop, we use Scanner method next() to extract // each token from the line, and each token is printed. // // Since there is no way to know how many tokens each line contains, we use // hasNext() - a boolean method of the Scanner class - to loop until the // current line has no more tokens. import java.util.Scanner ; public class EOFTest { public static void main (String args[]) { Scanner inputScan = new Scanner(System.in) ; System.out.println("Enter a sentence (or Q quit): ") ; String line = inputScan.nextLine() ; while ( ! line.equalsIgnoreCase("Q") ) // while more input lines... { // create a Scanner object to scan the line just read Scanner lineScan = new Scanner(line) ; System.out.println("\nHere are the tokens of the current line:\n") ; while ( lineScan.hasNext() ) // while line has more tokens... { String token = lineScan.next() ; // ...get next token System.out.println(token) ; // ...and print it } // print the tokenized line System.out.println("\nThe line just tokenized was:\n" + line + "\n") ; // prompt for and read next line System.out.println("Enter another sentence (or Q to quit): ") ; line = inputScan.nextLine() ; } } } /* sample output Enter a sentence (or Q quit): I had one grunch but the eggplant over there Here are the tokens of the current line: I had one grunch but the eggplant over there The line just tokenized was: I had one grunch but the eggplant over there Enter another sentence (or Q to quit): It's crackers to slip a rozzer the dropsy in snide! Here are the tokens of the current line: It's crackers to slip a rozzer the dropsy in snide! The line just tokenized was: It's crackers to slip a rozzer the dropsy in snide! Enter another sentence (or Q to quit): Q Press any key to continue... */