Solved

help with String tokenizer

Posted on 2004-08-25
9
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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