FileDocCategorySizeDatePackage
StrTokInts.javaAPI DocExample655Wed Jun 19 12:49:06 BST 2002None

StrTokInts.java

import java.util.StringTokenizer;

/** 
 * Given the problem of "I have a String with a fixed number of
 * Integers in it, how do I extract them?". Here's my best
 * readable solution.
 */
public class StrTokInts {
	public static void main(String[] args) {
		String in = "1  4   5   6   7  908  1231  23  53a";
		StringTokenizer st = new StringTokenizer(in);
		int iKnowHowMany = 9;
		for (int i=0; i<iKnowHowMany; i++) {
			String next = st.nextToken();
			try {
				int n = Integer.parseInt(next);
				System.out.println(i + " --> " + n);
			} catch (NumberFormatException ex) {
				System.out.println(i + " NOT A NUMBER (" + next + ")");
			}
		}
	}
}