How do access specific tokens with Java's StringTokenizer?
You can use String.split
for that instead of a StringTokenizer
.
String[] split = scrubbedInput.split(";");
split[2]; //index=2
dwwilson66
Visual Communications Professional with a background in technology. In the thick of learning Java, PHP & MySQL to augment my web skills. I'm taking a shine to programming a lot more than I thought I would.
Updated on August 01, 2022Comments
-
dwwilson66 over 1 year
I'm using Buffered Reader to pass individual lines of a file to Java's
StringTokenizer
. The file is structurd as follows:"2,0";"12345";"foo";"foo.doc" "2,4";"23456";"foo";"foo.doc";"34567";"foo7";"foo7.doc";"45678";"foo6";"foo6.doc";"56789";"foo5";"foo5.doc";"67890";"foo4";"foo4.doc" "3,0";"34567";"foo7";"foo7.doc" "3,0";"45678";"foo6";"foo6.doc" "3,0";"56789";"foo5";"foo5.doc" "3,0";"67890";"foo4";"foo4.doc"
Here's the code I'm using--so far.
public class parse { public static void main(String args[]) { FileInputStream inputStream = new FileInputStream("whidata0.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); while((scrubbedInput=br.readLine())!=null) { StringTokenizer strTok = new StringTokenizer(scrubbedInput, ";", false); int tokens = strTok.countTokens(); while (strTok.hasMoreTokens()) { tok01 = strTok.nextToken(); } System.out.println(" scrubbed: " + scrubbedInput); System.out.println(" tokens: " + tokens); System.out.println(" tok01: " + tok01); } } }
I need to be able to assign each token in a string to a variable to do additional manipulation. However, if I assign those variable in my
while
loop, the iteration will overwrite my variables, and they will all return with the same value.I'm trying to devide a way to do the following:
String token01 = strTok.tokenNumber(0); String token02 = strTok.tokenNumber(1); String token03 = strTok.tokenNumber(2); String token04 = strTok.tokenNumber(3); etc.
but cannot find any methods in the String Tokenizer documentation that will allow that. I can certainly write each line to a String array of
thisLineOfTokens[]
and use a for loop to createString tokenN = thisLineOfTokens[n]
, but is there a more direct way to access specific tokens?I'm kinda lost about the best way to reference a SPECIFIC token from my string.