aspose file tools*
The moose likes Beginning Java and the fly likes Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner" Watch "Scanner" New topic
Author

Scanner

Akanksha Joy
Greenhorn

Joined: Jun 05, 2009
Posts: 17

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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Akanksha Joy
Greenhorn

Joined: Jun 05, 2009
Posts: 17
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

Joined: Oct 13, 2005
Posts: 38896
    
  23
I think you are correct, yes.
 
 
subject: Scanner