StringTokenizer - not work!!!

publicclass lab6

{

publicstaticvoid main(String args[])

{

StringTokenizer s =new StringTokenizer("Windows Die Windows");

while (s.hasMoreTokens()){

System.out.println("Token: " + s.nextToken());

}

}

}

Why does this code not work?

[737 byte] By [Igorek] at [2007-9-30 21:36:43]
# 1
You forgot to import the StringTokenizer class... or atleas the java.util package...Add:import java.util.*; before the class definition and you should be able to compile and run the code...
fidofido at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 2
StringTokenizer is deprecated, so unless your developing on an older version of java you shouldn't be using it.
bjon045 at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 3
Maybe you should try String.split(). Just an idea/
DachshundThor at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 4
> StringTokenizer is deprecated, so unless your> developing on an older version of java you shouldn't> be using it.Can you provide a reference for that?Because I couldn't find a deprecation note in 1.4 nor in 1.5.
jschell at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 5
Okay so it's not deprecated, but it does say it's a Legacy class. I must have gotten mixed up at some point. Anyway, it's not recommended you use it.
bjon045 at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 6

> Okay so it's not deprecated, but it does say it's a

> Legacy class. I must have gotten mixed up at some

> point. Anyway, it's not recommended you use it.

Ah....in the docs for 1.5 it says....

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

And that of course is wrong.

It easily provable (as as been demonstrated in other threads on this board) that tokenizations is substantially faster than regex.

Which isn't too suprising given the nature of regexes and tokenization.

Someone should track down the benchmark that demonstrates this, then verify it on 1.5 and then post a bug report against the docs.

jschell at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 7
> And that of course is wrong.I fully agree./k1
komone at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 8

I recently reworked a bit of code which made reasonably heavy use of stringTokenizers to use string.split and performance wise it came out pretty much the same. I guess if your doing millions of lines of input and performance is critical then it could be an issue, but i did find string.split a tidy way of doing things.

public class HL7Parser {

private static String FIELD_SEPERATOR = "|";

private static String COMPONENT_SEPERATOR = "^";

private static String SUBCOMPONENT_SEPERATOR = "#";

private static String REPETITION_SEPERATOR = "~";

private static String ESCAPE_CHARACTER = "\\";

private Collection segments;

public HL7Parser(String hl7){

String[] aSeg = hl7.split(System.getProperty("line.separator"));

segments = new LinkedList();

FIELD_SEPERATOR = String.valueOf(aSeg[0].charAt(3));

COMPONENT_SEPERATOR = String.valueOf(aSeg[0].charAt(4));

REPETITION_SEPERATOR = String.valueOf(aSeg[0].charAt(5));

ESCAPE_CHARACTER = String.valueOf(aSeg[0].charAt(6));

SUBCOMPONENT_SEPERATOR = String.valueOf(aSeg[0].charAt(7));

for (int i = 0; i<aSeg.length; i++){

String[] fields = aSeg[i].split("\\"+FIELD_SEPERATOR,-1);

Object[] l = new Object[fields.length];

for (int i2 = 0; i2><fields.length;i2++){

String[] reps = fields[i2].split("\\"+REPETITION_SEPERATOR,-1);

Object[] l2 = new Object[reps.length];

for (int i3 = 0; i3><reps.length;i3++){

String[] comps = reps[i3].split("\\"+COMPONENT_SEPERATOR,-1);

Object[] l3 = new Object[comps.length];

for (int i4 = 0; i4><comps.length;i4++){

String[] subs = comps[i4].split("\\"+SUBCOMPONENT_SEPERATOR,-1);

l3[i4] = subs;

}

l2[i3] = l3;

}

l[i2] = l2;

}

segments.add(l);

}

}

}

public static void main(String[] args){

HL7Parser test = new HL7Parser("MSH|^~\\#|TC-PAS|TC|||20040916152940||ADT^A28|5979416-16|P|2.4|||AL|NE||||||2.4"+System.getProperty("line.separator")+"PID|||");

}

>

bjon045 at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...
# 9

>

> I recently reworked a bit of code which made

> reasonably heavy use of stringTokenizers to use

> string.split and performance wise it came out pretty

> much the same.

And as I suggested above there is a bit of code somewhere that demonstrates a significant difference.

jschell at 2007-7-7 3:07:30 > top of Java-index,Administration Tools,Sun Connection...