Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tokenizing with scanner

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Can any one explain me the code and output i am not getting it
its on pg.no 488 K&B Book


import java.util.Scanner;
class ScanNext {
public static void main(String [] args) {
boolean b2, b;
int i;
String s, hits = " ";
Scanner s1 = new Scanner(args[0]);
Scanner s2 = new Scanner(args[0]);
while(b = s1.hasNext()) {
s = s1.next(); hits += "s";
}
while(b = s2.hasNext()) {
if (s2.hasNextInt()) {
i = s2.nextInt(); hits += "i";
} else if (s2.hasNextBoolean()) {
b2 = s2.nextBoolean(); hits += "b";
} else {
s2.next(); hits += "s2";
}
}
System.out.println("hits " + hits);
}
}
If this program is invoked with
% java ScanNext "1 true 34 hi"
it produces
hits ssssibis2
 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its quite nicely explained in the book itself. It would be helpful if you tell which part you need more clarification for.
 
Collins Mbianda
Ranch Hand
Posts: 259
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srinivas !!!

I think the Scanner concept is new for you.
I was also new for me when i read your post.

I think the better way to learn is to try to understand the whole
concept first, and then read the API as Jesper Young suggest you
in another post.
This article could be of interest:
SCANNING TEXT WITH JAVA.UTIL.SCANNER

Regards,
Collins
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Collins,

Thanks for your site it really helped me in understanding the concept


Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic