?
Solved

help with String tokenizer

Posted on 2004-08-25
9
Medium Priority
?
243 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
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

Technology Partners: 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!

Question has a verified solution.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 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