• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tokenizing with Scanner

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() ?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:
...



Got the same output. "si"
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "pointer" is always before anything and will move after the next point after you use next.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys!
 
brevity is the soul of wit - shakepeare. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic