• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Scanner doubt from Paul's book

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default delimiter is space.
But there is no space in str input, why the output is a,b,cd,420??

 
Enthuware Software Support
Posts: 4896
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think it should print?
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Anil:
What do you think it should print?



Hi Paul,

well I'm expecting that it won't go into the loop, print nothing/empty because scanner doesn't do splitting then hasNext() should return false.

but when run it, it print a,b,cd,420
 
Paul Anilprem
Enthuware Software Support
Posts: 4896
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your doubt.

The short answer is that if there is no match, then the whole input is returned as the only token. However, this has not been explicitly stated in the API. Why? Read further...

Now, the long answer:
The problem is in understanding the semantics of the word "delimiter". There is a difference in the usage of a pattern as a "delimiter" and as a "search string".

Consider the string "a b". How many tokens will the default scanner return? Or in other words, how many times the loop (of the above program) should be entered?

The scanner starts building (in its buffer) a token right from the first char of the input. It keeps looking (and appending the inspected char to the token) until it finds the delimiter. As soon as it finds the delimiter, it returns whatever token had built up, empties its buffer, and starts looking again. At this point, the input ends. So it simply returns whatever it had in the token buffer, which is the rest of the string after the last occurance of the delimiter.

Now, how many times the string " " occurs in the given string? Only once. So if you use the Pattern/Matcher classes, you will get only one match.

HTH,
Paul.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anth,
As you know the default delimiter is white space for a Scanner.
So when you pass the String which in this case is a single word,the scanner.hasNext method returns true for the first time so it goes inside the loop and prints the value using next() method.
After this when it goes into second time this time the while(false) since there is no other word present in the string ...

Hope this explains you something
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm I got it now. Thanks Paul and Santosh!! my head is clear now.
 
I am going to test your electrical conductivity with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic