JAVA Beginner
JAVA Beginner

Reputation: 481

How to get an required String using String Tokenizer

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

Upvotes: 3

Views: 283

Answers (2)

mishadoff
mishadoff

Reputation: 10789

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

Upvotes: 3

Masudul
Masudul

Reputation: 21961

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(':')));
  }

Upvotes: 2

Related Questions