The following code demonstrates how to analyze a keyboard input in terms of tokens. The client may input no more than 100 words with the final string “end” (excluded). These words are simultaneously stored in the String array s, through which one may do search for a specific word.
import java.util.*;
public class Tokenizer0 {
public static void main(String args[]){
Scanner asdf = new Scanner(System.in); // obtain an instance of Scanner
String s []= new String[100]; // declare a string array with the size of 100 to accommodate the input words via asdf
int counter = 0; // counter counts the number of input words
while(asdf.hasNext()){ //returns true if asdf has next token
s[counter++]=asdf.next(); // assign the next token to the string array
if (s[counter-1].compareTo("end")==0) break; // check the input word. if the input is "end", then stop the while loop
}
System.out.println("Number of Words:" + (counter-1)); // print out the total number of input words
for (int i=0; i<counter-1; i++) // print out the input words
System.out.println(s[i]);
}
}