Re: StreamTokenizer question
On Jun 4, 7:48 pm, Arne Vajh=F8j <a...@vajhoej.dk> wrote:
package june;
import java.util.StringTokenizer;
public class ST {
public static void main(String[] args) {
String s = "08/22/05 15:18:47 21.00 0.00 0.00 -0.113 -1861.3 3=
97.7 169.4 38.8 8.7";
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
String[] parts = s.split(" ");
for(int i = 0; i < parts.length; i++) {
System.out.println(parts[i]);
}
}
}
The split method throws PatternSyntaxException if the regular
expression's syntax is invalid. It doesn't change much, but I prefer
to consider the possibility of catching the exception:
import java.util.regex.PatternSyntaxException;
public class Main {
public static void main(String[] args) {
String str = "08/22/05 15:18:47 21.00 0.00 0.00 " +
"-0.113 -1861.3 397.7 169.4 38.8 8.7";
try {
String[] tokens = str.split(" ");
for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}
} catch (PatternSyntaxException e) {
System.err.println(e.getMessage());
}
}
}
The result of this little program is:
08/22/05
15:18:47
21.00
0.00
0.00
-0.113
-1861.3
397.7
169.4
38.8
8.7
And, as Arne said, you can deal with that if you need to.
Regards,
Juan Pedro Villa