FileDocCategorySizeDatePackage
StrTokDemo4.javaAPI DocExample1366Thu Mar 25 20:50:52 GMT 2004None

StrTokDemo4

public class StrTokDemo4 extends Object
Show using a StringTokenizer including getting the delimiters back

Fields Summary
public static final int
MAXFIELDS
public static final String
DELIM
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] a)

		printResults("A|B|C|D", process("A|B|C|D"));
		printResults("A||C|D", process("A||C|D"));
		printResults("A|||D|E", process("A|||D|E"));
	
public static voidprintResults(java.lang.String input, java.lang.String[] outputs)

		System.out.println("Input: " + input);
		for (int i=0; i<outputs.length; i++)
			System.out.println("Output " + i + " was: " + outputs[i]);
	
public static java.lang.String[]process(java.lang.String line)
Processes one String, returns it as an array of Strings


	           
	     
		String[] results = new String[MAXFIELDS];

		// Unless you ask StringTokenizer to give you the tokens,
		// it silently discards multiple null tokens.
		StringTokenizer st = new StringTokenizer(line, DELIM, true);

		int i = 0;
		// stuff each token into the current slot in the array.
		while (st.hasMoreTokens()) {
			String s = st.nextToken();
			if (s.equals(DELIM)) {
				if (i++>=MAXFIELDS)
					// This is messy: See StrTokDemo4b which uses 
					// a List to allow any number of fields.
					throw new IllegalArgumentException("Input line " +
						line + " has too many fields");
				continue;
			}
			results[i] = s;
		}
		return results;