• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want to know more about Delimiters.

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10432
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10432
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got your point. Thank you very much
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic