String Tokenizer

Notes courtesy of Avo Shahverdian

Hey John,

I thought you should teach your students about StringTokenizers.  I think they can be very useful.  
The two methods of splitting strings into individual words I pasted below, along with the link to the oracle page.  
I suggest you skip the tokenizer method and go straight to he "split" method which lies under the "String" class.  See Below for both examples.  

I hope all is good, and just so you know, if any of your students have any questions that you're not sure about, they can always email me.  
I'm happy to help, either with code, or with systems theory (the other stuff).  


The following is one example of the use of the tokenizer. The code:

     StringTokenizer st = new StringTokenizer("this is a test");      while (st.hasMoreTokens()) {          System.out.println(st.nextToken());      }  

prints the following output:

     this      is      a      test  

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

     String[] result = "this is a test".split("\\s");      for (int x=0; x<result.length; x++)          System.out.println(result[x]);  

prints the following output:

     this      is      a      test