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]

> 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.
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|||");
}
>