//TokenizerTest.java //demo StringTokenizer to input 3 ints at once //and to parse a string of "words". //StringTokenizer to get the pieces ("tokens") of a string. import java.util.*; //StringTokenizer import javax.swing.*; public class TokenizerTest { public static void main(String[] args) { String input = JOptionPane.showInputDialog( "Enter three integers (separated by spaces)" ); //make a StringTokenizer object from the input String: StringTokenizer twoInts = new StringTokenizer(input); //any number of spaces before, between, after the numbers OK. //each "token" is a String. int num1 = Integer.parseInt(twoInts.nextToken()); //gets next (first) "token", convert to int int num2 = Integer.parseInt(twoInts.nextToken()); //gets next (second) "token" int num3 = Integer.parseInt(twoInts.nextToken()); //gets next (third) "token" System.out.println("First=" + num1 + " Second=" + num2 + " Third=" + num3); String s="here:is:a: bunch:of:words:separated by:colons!"; StringTokenizer st = new StringTokenizer(s,":"); //optional 2nd parameter is delimiter (separator) (default is space) while (st.hasMoreTokens()) //iterate over the tokens System.out.println(st.nextToken()); //each call of nextToken() gets the next token (word) } }