Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do access specific tokens with Java's StringTokenizer?

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 create String 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.

like image 588
dwwilson66 Avatar asked Dec 28 '25 01:12

dwwilson66


1 Answers

You can use String.split for that instead of a StringTokenizer.

String[] split = scrubbedInput.split(";");

split[2]; //index=2
like image 140
Gangaraju Avatar answered Dec 30 '25 17:12

Gangaraju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!