944,196 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5554
  • Java RSS
Apr 24th, 2006
0

StringTokenizer problem

Expand Post »
Hello,

I'm currently having some problems with the StringTokenizer from
java util.

I've declared a StringTokenizer like:

StringTokenizer token = new StringTokenizer(line,"|");

to token out a string read from a txt file such as:

Diablo|RPG|PG|PS2|20

and it works fine(detects all the "|" as delimeters)


The problem now is, if I have lines with spaces like:

C&C Generals|Real Time Strategy|PG|PS2|20

it detects the spaces as well and the tokenizing is out.

So, I'm wondering if anyone out there can help me solve this problem. I just want the tokenizer to recognize "|" as a delimiter and not anything else.

Thanks in advance.
Similar Threads
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
Apr 24th, 2006
0

Re: StringTokenizer problem

How bout this?


Tokens.java
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Tokens
  5. {
  6.  
  7. public static void main(String[] args)
  8. {
  9. int count=0;
  10. String array[]= new String[100];
  11. StringTokenizer st = new StringTokenizer("Diablo & generals|RPG|PG|PS2|20","|");
  12. while (st.hasMoreTokens())
  13. {
  14. //System.out.println(st.nextToken());
  15. array[count] =st.nextToken();
  16. count++;
  17. }
  18. System.out.print(array[0]); //change here to see different token
  19. }
  20. }

http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld
[Tis simple as Pie]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 24th, 2006
0

Re: StringTokenizer problem

String line = "C&C Generals|Real Time Strategy|PG|PS2|20";
String[] tokens = line.split("|");
for (int i=0;i<tokens.length;i++)
     System.out.println(tokens[i]);
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Apr 25th, 2006
0

Re: StringTokenizer problem

Yup.... I'm using the

line.split("|",5)

Found that in the Sun Java forum. I can forget bout StringTokenizer now,
cause the split method is definitely much more cooler...:mrgreen:

And the StringTokenizer is not recommended as of 5.0 I think...:-|

Well...forced to do my project in Java... if it were in Delphi I can finish it with 1/3 of the time I spent using Java.

Thanks for the replies guys...really helped
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
Apr 25th, 2006
0

Re: StringTokenizer problem

>And the StringTokenizer is not recommended...

What is all this hoo-ha, about using split instead of stringTokenizer. Can anyone explain what I should be using and why?

And what of the subsequent link?
http://www.stabile.org/blog/2005/11/...etc-technical/



http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld
[Tis simple as Pie]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 25th, 2006
0

Re: StringTokenizer problem

It depends on the situation I guess.

For mine it works just fine. No performance drop compared to StringTokenizer, plus it works better... since there's no more need to create an array or arraylist to store the stuff and it ignores the spaces in between data...plus you get to specify how many tokens you want from each line as a second argument. Also you get to save quite a number of lines.

Was having problems with StringTokenizer cause it detects the spaces as delimeters as well though I specified that the delimeter is only "|".

As for the link, I think it's full of crap. Some people just can't switch to something new where they're already comfortable with the old one. Getting memory leaks in Java is quite impossible, cause that's what a garbage collector is for. You should see how memory leaks are like in Delphi...your com doesn't hang but it moves very very very slow till you reboot...if not it'll hang indefinitely. Looping a 100,000,000 times oh my...
:cheesy:
Reputation Points: 13
Solved Threads: 1
Light Poster
j1979c is offline Offline
42 posts
since Sep 2005
Apr 25th, 2006
0

Re: StringTokenizer problem

That link makes some valid points about initialization costs. I find splitString easier to use though, and it does more than just search for delimiters, its parameter is actually a regular expression. The regex search could possibly slow the parsing down a bit, but I'm not sure by how much or how much it would even take to be noticable.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Mar 17th, 2008
0

Re: StringTokenizer problem

Split is simple.
It follows the regular expression.
Anyways for the above code there is slight change

String line = "C&C Generals|Real Time Strategy|PG|PS2|20";
String[] tokens = line.split("\\|");
for (int i=0;i<tokens.length;i++)
System.out.println(tokens[i]);

janarjan
Reputation Points: 10
Solved Threads: 0
Newbie Poster
janarjan is offline Offline
1 posts
since Mar 2008
Feb 28th, 2010
0

StringTokenizer

give me the sample output that you want to be output
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xirkhan is offline Offline
1 posts
since Feb 2010
Mar 1st, 2010
0
Re: StringTokenizer problem
Click to Expand / Collapse  Quote originally posted by xirkhan ...
give me the sample output that you want to be output
It has been 2 years since the last post. You think that the OP was waiting for you?
If you really want to help someone then why don't you try the most recent posts?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Menu and Input Confirmation
Next Thread in Java Forum Timeline: Newbee and Needs help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC