How to get an required String using String Tokenizer

298 Views Asked by At

Im working with String Tokenizer API. Im not using Split() because Im working with jdk 1.3.

I have an input String which is given below

String input="Open_filedesc_count:mix:-1:-1:longterm:HML Max_filedesc_count:mix:-1:-1:longterm:HML,Percent_usage:mix:-1:95/90/85:standard:HML, Availability:mix:1/-/-:-1";

Now i would like to tokenize the string , The output should be like

Open_filedesc_count
Percent_usage
Availability

It simply eliminates most of the strings. but i want the output as mentioned above.

I tried three type of constructors but couldnt get the output as mentioned format

2

There are 2 best solutions below

1
On BEST ANSWER
StringTokenizer tokenizer = new StringTokenizer(input, ",");
while (tokenizer.hasMoreElements()) {
    StringTokenizer tokenizer2 = new StringTokenizer(tokenizer.nextToken(), ":");
    System.out.println(tokenizer2.nextToken());
}
0
On

Try,

String input = "Open_filedesc_count:mix:-1:-1:longterm:HML
                Max_filedesc_count:mix:-1:-1:longterm:HML,
                Percent_usage:mix:-1:95/90/85:standard:HML,
                 Availability:mix:1/-/-:-1";

  StringTokenizer tokenizer = new StringTokenizer(input, ",");

  while(tokenizer.hasMoreTokens()){          
    String token=tokenizer.nextToken();
    System.out.println(token.substring(0, token.indexOf(':')));
  }