[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

help with String tokenizer

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

 
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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