• 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

Scanner class

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Devaka Coory's Simulator:


Output is :100,55,
If I remove 3l then output is :100,55,62127,


Can anyone please explain the output..
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner class splits the source by " " (this is a default behavior)

So it's going to display until a non long value be found.. ( on this case because the letter 'l' the while loop finishes executing).
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But 3l is a long value. So hasNextLong() is still true.Why does loop end..
May be its really very easy question. But i just did not get it.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daisy Lakhanpal:
But 3l is a long value. So hasNextLong() is still true.Why does loop end..
May be its really very easy question. But i just did not get it.



You have to distinguish the parsers. Just because the Java compiler understands the 3L to mean a long value, doesn't mean the Scanner parser does. Notice that it also doesn't understand hexicimal, octal, or expressions -- the nextLong() method is just a dumb numeric parser.

[Don't want to belittle the Scanner class, as it does have methods that support radixes. The point of the argument is that you need to understand what the Scanner class is looking for -- not what the Java compiler is looking for.]

Henry
[ December 13, 2008: Message edited by: Henry Wong ]
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for the light.
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that this topic is old but can you tell me why the value 0100 becomes 100?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any unnecessary zeros before a number are automatically removed. See the code below
 
Mark Moge
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks. But it seems to be a bit confusing for example:
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Moge wrote:Ok thanks. But it seems to be a bit confusing for example:



The Scanner class won't do anything with leading zeros because it doesn't have any meaning.

To the compiler, leading zero means that the value is to be parsed as an octal.

Henry
 
Mark Moge
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I can see how it works now. Thanks guys
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic