File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Tokenizing with scanner

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225

Can any one explain me the code and output i am not getting it
its on 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 =; hits += "s";
while(b = s2.hasNext()) {
if (s2.hasNextInt()) {
i = s2.nextInt(); hits += "i";
} else if (s2.hasNextBoolean()) {
b2 = s2.nextBoolean(); hits += "b";
} else {; 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:


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
I agree. Here's the link:
subject: Tokenizing with scanner
jQuery in Action, 3rd edition