String Tokenizer

You will have noticed in the last assignment that we could only handle single words. How do we handle more than one word? Java has a class to do this: StringTokenizer in java.util.StringTokenizer.

This code shows a method that takes a string and prints out each word with an enter after it.

import java.io.*;
import java.util.StringTokenizer;

public class StringTokens
{

public static void main (String args [])
{

stringo = "I am a really really really really really really really long string. Yes I am. A really really really really really long string. Too long to fit in the output window. I'd stop reading now. There is nothing of value beyond this point. Really. Stop now.";
Tokenizer(stringo);

}

public static void Tokenizer (String s)
{
//A new string tokenizer - pass the string to manipulate to it
StringTokenizer tokenizer = new StringTokenizer (s);
//countTokens counts the number of words in the string
int numberOfWords = tokenizer.countTokens ();

//loop for number of words in the string
for (int i = 0 ; i < numberOfWords ; i++)
{

//Peel off each word and print it with an enter afterwards
String nextword = tokenizer.nextToken (); //peel off next word
System.out.println (nextword);

}

}

Note the following pieces of code:
StringTokenizer tokenizer = new StringTokenizer (s);
tokenizer.countTokens ();
tokenizer.nextToken ();

This code takes a string with doubles in it (eg. "2 3 45 67 1 344") and returns a double[] from the string:

/* Parses the numbers from the String
* From Horstmann, Cay. Java 2 Essentials. Second Edition. pg 491
* @param input should contain numbers with spaces between them
* @return double [] an array of the numbers parsed from input
*/
public static double [] getData (String input) throws java.lang.NumberFormatException
{

StringTokenizer tokenizer = new StringTokenizer (input);
double [] data = new double [tokenizer.countTokens ()];
for (int i = 0 ; i < data.length ; i++)
data [i] = Double.parseDouble (tokenizer.nextToken ());
return data;

}