| Author |
Want to know more about Delimiters.
|
sonu raj
Ranch Hand
Joined: Jul 31, 2012
Posts: 33
|
|
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();
output:
1
2
red
blue
I know how to use Delimiter but I just want to figure out why we write \\s* before and after the delimiter?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4734
|
|
sonu raj wrote:I know how to use Delimiter but I just want to figure out why we write \\s* before and after the delimiter?
Because the useDelimiter() method takes a regular expression (regex) as its argument, and in a regex "\\s" means "a whitespace character". The '*' means "0 or more", so I would say that the regex is actually wrong; it should be "\\s+" ('+' means 1 or more).
Winston
PS: For more information, have a look at the documentation for java.util.regex.Pattern (←click), or look at the Java Tutorials.
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
I think there is nothing wrong with that delimiter. It is simply different from \\s+fish\\s+
Try thisTry changing the \\s* to \\s+ or \\s and see the difference. I missed out a space or two so as to accentuate the difference. I usually warn against using \r and \n in Strings, but have used them here because they are kinds of whitespace.
Any Irishman can tell you why I called the Scanner murphy
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4734
|
|
Campbell Ritchie wrote:I think there is nothing wrong with that delimiter. It is simply different from \\s+fish\\s+
Nope, you're quite right; my apologies sonu.
@sonu: Let me put it another way: I suspect that what you'll want is "\\s+fish\\s+"; but Campbell's absolutely right, you should test to make sure.
Winston
|
 |
sonu raj
Ranch Hand
Joined: Jul 31, 2012
Posts: 33
|
|
Got your point. Thank you very much
|
 |
sonu raj
Ranch Hand
Joined: Jul 31, 2012
Posts: 33
|
|
Campbell Ritchie wrote:I think there is nothing wrong with that delimiter. It is simply different from \\s+fish\\s+
I tried but still confused. I still don't know how \\s* is different from \\s+ ?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
sonu raj wrote:
Campbell Ritchie wrote:I think there is nothing wrong with that delimiter. It is simply different from \\s+fish\\s+
I tried but still confused. I still don't know how \\s* is different from \\s+ ?
The Oracle/Sun Tutorial on Regular Expressions may be a good place to start....
http://docs.oracle.com/javase/tutorial/essential/regex/
Regular expressions is not something that you should just skim over -- arguably, it is a language of its own.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Want to know more about Delimiters.
|
|
|