aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tokenizing with scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tokenizing with scanner" Watch "Tokenizing with scanner" New topic
Author

Tokenizing with scanner

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
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


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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

Joined: Aug 11, 2007
Posts: 259
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


SCJP 5.0 | SCWCD 1.4
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
hi Collins,

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


Thanks again
 
 
subject: Tokenizing with scanner