Created
April 17, 2019 23:25
-
-
Save luisenriquecorona/e565151822fee2ce74856368bf4e7d97 to your computer and use it in GitHub Desktop.
Show using a StringTokenizer including getting the delimiters back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** Show using a StringTokenizer including getting the delimiters back */ | |
public class StrTokDemo4 { | |
public final static int MAXFIELDS = 5; | |
public final static String DELIM = "|"; | |
/** Processes one String; returns it as an array of Strings */ | |
public static String[] process(String line) { | |
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 Vector to allow any number of fields. | |
throw new IllegalArgumentException("Input line " + | |
line + " has too many fields"); | |
continue; | |
} | |
results[i] = s; | |
} | |
return results; | |
} | |
public static void printResults(String input, 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 void main(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")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment