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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tokenizing with Scanner" Watch "Tokenizing with Scanner" New topic
Author

Tokenizing with Scanner

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi there!

This is a code from K&b.
Can some one help me to explain how this program running.
that is the sequence of the statements getting executed.
I am getting a little confused.



The input is "1 true 34 hi"
And Answer is hits ssssibis2.

What about the space between the words in the input.
Is that considered as a valid space \s or is it considered that the value is null.
Will it hold true for hasNext() ?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

The input is "1 true 34 hi"
And Answer is hits ssssibis2.


are you sure this is what you are getting at your side ,

When I complied the above program , I got the following output :

Entering the First Condition
Entering the Second Condition
Hits si


The final result is "si"..

and about "space" , the scanner class acts as :

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Sagar Rohankar:
...


Got the same output. "si"


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
You are supposed to put "1 true 34 hi" in quotes at the command prompt.

java ScanNext "1 true 34 hi"

You get two different answers with quotes and with out quotes - that is another confusion, why the quotes makes a difference!

Try it out and let know...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I have slighlty modified the code - just one line.It is in bold.


With the modifications the result is ssssibis2
And with out any modification it is ssssitrueis2.

Shouldn't make much difference.

[ July 30, 2008: Message edited by: Nabila Mohammad ]

[ July 30, 2008: Message edited by: Nabila Mohammad ]
[ July 30, 2008: Message edited by: Nabila Mohammad ]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Using quotes we will have only 1 argument, its all the phrase, thats why it changes :-)

Print the args[0] using quotes and without quotes. See the diference.
[ July 30, 2008: Message edited by: Raphael Rabadan ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Yeah I got it! Thanks.
Just figured it out.

Tried this code with some additions and it helped me to understand better



But I still do have few questions.
Is the pointer always Before the first character or it just in this case.
Because the first time I was thinking how the prgram was executed, i thought the pointer was At 1 , and not before it.

When it is at 1 , then the hasNext() would return a space (which would return false) and the the loop would get over.
But it didn't - so looks like the space doesn't return a false and it was not at 1 but before it.
[ July 30, 2008: Message edited by: Nabila Mohammad ]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
The "pointer" is always before anything and will move after the next point after you use next.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Nabila,
so looks like the space doesn't return a false
If your input string is "1 true 34 hi", you can think of the scanner as consisting of

1. a sequence of tokens (the delimters - spaces in your case - are removed). In your sample: {1,true,34,hi}

2. a pointer p. At first p points to 1. Application of next() returns the token to that p points and lets p point to the next token.

But note: That's just a help for understanding. Actually the sequence in 1. is produced during usage of the different next methods.


SCJP 5 (98%) - SCBCD 5 (98%)
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks Guys!
 
 
subject: Tokenizing with Scanner