• 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

parseInt (String s, int radix)

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain how this method in the Integer class works. I understand the parseInt (String s) method no problem but I guess I am having trouble understanding what a radix is. I've included some examples from the API to demonstrate the results of calling this method. Please help ... Gerry.
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"radix" is also known as "number base". For example, normal decimal numbers are base 10, binary numbers are base 2, octal numbers are base 8, hexadecimal numbers are base 16 etc. The wierdness comes with number bases above 10. The convention is to start using letters for digits. The binary digits are 0 and 1, the octal digits are 01234567, the hexadecimal "digits" are 0123456789ABCDEF and so on. In base 36, for example, the digits are 0-9 and all the letters of the alphabet!
Has this helped?
 
Gerry Timmermans
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that helps immensely. I think I get the jist of it but I'm still confused why the example from the API shows
parseInt("2147483648", 10) throws a NumberFormatException. (1)
while
parseInt("2147483647", 10) doesn't. (2)
All the numbers in the String (1) belong to the subset of radix 10 do they not? I'm still missing something.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The slightly larger number fails because it is too large for an int! In Java, an int is a signed 32-bit value, allowing values from -2147483648 to 2147483647 To hold any number larger than that you need to put it into a long.
 
Gerry Timmermans
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I missed that. Thanks very much for your help ... at least it's clearer than mud now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic