| | |
StringTokenizer problem
![]() |
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.
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.
How bout this?
Tokens.java
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
Tokens.java
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; class Tokens { public static void main(String[] args) { int count=0; String array[]= new String[100]; StringTokenizer st = new StringTokenizer("Diablo & generals|RPG|PG|PS2|20","|"); while (st.hasMoreTokens()) { //System.out.println(st.nextToken()); array[count] =st.nextToken(); count++; } System.out.print(array[0]); //change here to see different token } }
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
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]);
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
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
>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]
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]
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:
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:
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
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.
![]() |
Similar Threads
- Best Way to Check for same Word in string? (Java)
- Txt Coordinates into chart... (Java)
- StringTokenizer (Java)
Other Threads in the Java Forum
- Previous Thread: Need help with T/F and Multiple Choice
- Next Thread: Enter the netbeans any good beginner stuff?
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






