?
Solved

help with String tokenizer

Posted on 2004-08-25
9
Medium Priority
?
252 Views
Last Modified: 2010-03-31
i have a string

 2d 1 3d 1 animation 1 curve 1 graphic 1

and i want to break them into tokens and leaving out the numbers i know you could do this by

StringTokenizer token = new StringTokenizer(string,"1234567890");

but that removes the 2 in 2d which i want to keep intact can anyone help
            
0
Comment
Question by:rimmer007
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11898352
Try a StreamTokenizer instead.
0
 
LVL 92

Expert Comment

by:objects
ID: 11898355
0
 
LVL 92

Expert Comment

by:objects
ID: 11898359
If you need to use a StringTokenizer then use:
StringTokenizer token = new StringTokenizer(string);

and then use Integer.parseInt() to check if each token is a number or not.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Accepted Solution

by:
armoghan earned 252 total points
ID: 11899232
String a = "2d 1 3d 1 animation 1 curve 1 graphic 1";
    StringTokenizer token = new StringTokenizer(a);
    while(token.hasMoreTokens())
      {
        try{
          int i = Integer.parseInt(token.nextToken());
          System.out.println(""+i);
        }catch (Exception e)
        {
       
        }
      }
0
 

Expert Comment

by:aniket_pcs
ID: 11902920
Just to confirm, does the string have space between the two words/characters, i.e "2d 1 3d 1".

If yes, we can use the space " " as a delimiter?
just a basic workaround

String l_Str = "2d 1 3d 1 animation 1 curve 1 graphic 1";

StringTokenizer l_Tok = new StringTokenizer(l_Str, " ");
while (l_Tok.hasMoreElements()) {
      String l_Elem = (String)l_Tok.nextElement();

      if(l_Elem.length()>1) {
            System.out.println(l_Elem);
      }
}
0
 

Author Comment

by:rimmer007
ID: 11923848
hi

i using armoghan method and i m trying to print out the strings

i put this

  System.out.println("String  " + Integer.toString(i));
     

inside the catch but numbers are printed what am i doin wrong?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 248 total points
ID: 11923961
you can't print out the value using that code, instead try:

String s = "2d 1 3d 1 animation 1 curve 1 graphic 1";
StringTokenizer i = new StringTokenizer(s);
while (i.hasMoreTokens())
{
    try
    {
          String token = i.nextToken();
          int n = Integer.parseInt(token);
          System.out.println(""+n);
    }
    catch (Exception e)
    {
        System.out.println("String  " + token);
    }
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question