Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner

 
Akanksha Joy
Greenhorn
Posts: 17
  • 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 is :
1
2
red
blue

Can anyone help me in understanding how the output has come? I have not understood the core thing:
" ("\\s*fish\\s*"); "
what is the meaning of \s,\d??
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akanksha Joy wrote:
I have not understood the core thing:
" ("\\s*fish\\s*"); "
what is the meaning of \s,\d??


Basically, the parameter is a regular expression. Take a look the java.util.regex.Pattern class for more info. Or pick up a book, as regular expressions is something that every devloper should learn.

Henry
 
Akanksha Joy
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken a look and tried some examples. So in the above example:
("\\s*fish\\s*");
This string is treated as the delimiter: "zero or more times white space, then word fish, then zero or more white spaces".
Am I right?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct, yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic