Solved

help with String tokenizer

Posted on 2004-08-25
9
199 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
9 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Accepted Solution

by:
armoghan earned 63 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 62 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now