Click to See Complete Forum and Search --> : java.util.Scanner
Dillinger4
Dec 2nd, 2004, 12:25 PM
I started taking a look at some of the new classes in Java5 but i am a bit confused on how a Scanner uses delimiters. For instance take the following block of code.
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
What is the "\\s*" used for? If the delimiter was
specified as useDelimiter("fish"); wouldn't the output
still be the same?
CornedBee
Dec 2nd, 2004, 04:19 PM
The delimiter is any number of whitespace, then "fish", then again any number of whitespace.
With the current code, the output is
"1"
"2"
"red"
"blue"
(Lines intentionally wrapped in quotes for demonstration purposes.)
If you change the delimiter to "fish" the output will be
"1"
"2"
" red "
" blue "
Or perhaps it will even fail on nextInt.
Dillinger4
Dec 3rd, 2004, 12:44 PM
Ok according to the java.util.regex.Pattern documentation. "\\s*fish\\s*" contains what sun calls a greedy quantifier(X* X, zero or more times) and \s A whitespace character: [ \t\n\x0B\f\r]. I always thought parsing was done independent of the amount whitespace. :confused: Ive only ever tried to parse Strings using a StringTokenizer with single spaces between tokens.
CornedBee
Dec 3rd, 2004, 01:39 PM
If you define that your parser should ignore whitespaces, then it will, but nothing in the definition of parser says so. A Scanner doesn't, by default, unless you tell it to by giving whitespace in the separator pattern.
Dillinger4
Dec 4th, 2004, 12:00 PM
Ok i see. You can use a Scanner to parse by white space independent of the amount of whitespace by using a greedy quantifer. Seems a lot more flexible then using a StringTokenizer where you can parse by space but not ignore the amount of space used. :eek:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.