• 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

Integer.parseInt

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main (String[] args) {
System.out.print(Integer.parseInt("11", 2)+", ");
System.out.print(Integer.parseInt("11", 8)+", ");
System.out.print(Integer.parseInt("11", 10)+", ");
System.out.print(Integer.parseInt("11", 16));
}
Could anyone explain me why output is 3,9,11,17. Pl explain me about radix.
Thanks.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
radix means the base in which you are counting.
Binary representation of numbers uses base 2.
Octal representation of numbers uses base 8.
Decimal representation of numbers uses base 10.
Hexedecimal representation of numbers uses base 16.
Now,
11 in base 2 can be seen as
00000000 00000000 00000000 00000011
whose decimal value is 3
11 in base 8 can be seen as
011 (the first 0 denotes an octal literal)
and in binary
00000000 00000000 00000000 00001001
whose decimal value is 9
11 in base 10 can be seen as
11
whose decimal value is 11
(that's the way we, human, usually compute things)
11 in base 16 can be seen as
0x00000011
and in binary
00000000 00000000 00000000 00010001
whose decimal value is 17
There are some better explanations as well as a number converter on the following link : http://www.cut-the-knot.com/binary.shtml
[ August 27, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Valentine, for the great explaination.
I found that Byte, Short, Integer and Long all have parseXXX(String s, int radix) method. I tested with a String and found that they return the same output.

I try different values of the String and these are what I think happen:
1. parseXXX() method checks for the range of the String at runtime. If the string contains integer value greater than XXX.MAX_VALUE or less than XXX.MIN_VALUE, NumberFormatException is thrown.
2. String s must contains only digits 0-9. And if the digit is not in an acceptable range of the radix, NumberFormatException is thrown at runtime. i.e. with the above example, the exception is thrown if the radix is 2 instead of 8 because base 2 contains digits 0 and 1 only.
Am I right?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by May Pat:
Thanks a lot, Valentine, for the great explaination.
I found that Byte, Short, Integer and Long all have parseXXX(String s, int radix) method. I tested with a String and found that they return the same output.

I try different values of the String and these are what I think happen:
1. parseXXX() method checks for the range of the String at runtime. If the string contains integer value greater than XXX.MAX_VALUE or less than XXX.MIN_VALUE, NumberFormatException is thrown.
2. String s must contains only digits 0-9. And if the digit is not in an acceptable range of the radix, NumberFormatException is thrown at runtime. i.e. with the above example, the exception is thrown if the radix is 2 instead of 8 because base 2 contains digits 0 and 1 only.
Am I right?



An exception of type NumberFormatException is thrown if any of the following situations occurs:
The first argument is null or is a string of length zero.

The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.

The value represented by the string is not a value of type int.


The maximum radix specified by Character.MAX_RADIX is 36. Please note that ten numberic digits plus 26 letters equals 36. Therefore, all ten digits and all 26 letters can be used if you are parsing a base 36 value.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, for example
System.out.print(Integer.parseInt("z", 36));
prints 35.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Val, Dan, Ron:
I played with this code little bit. My finding is
that all of the following methods have same behavior:
Byte.parseByte(String s, Radix n)
Short.parseShort(String s, Radix n)
Long.parseLong(String s, Radix n)
Integer.parseInt(String s, Radix n)
So if you provide same s and n to above, you will
get same result.

They all return 12. If they are identical methods, why not one method at Object level only?
Thanks
Barkat
[ August 27, 2002: Message edited by: Barkat Mardhani ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the API documentation again -- each of those methods has a different return type.
And try changing "12" to "1200" or "120000".
[ August 27, 2002: Message edited by: Ron Newman ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original posted by Ron:


Check the API documentation again -- each of those methods has a different return type.
And try changing "12" to "1200" or "120000".


I think all mentioned methods have same return type i.e. integral value but allowable range of values that string can represent is limited by which method we are calling. So for Byte.parseByte method string should not represent more than 127.
Thanks
Barkat
[ August 27, 2002: Message edited by: Barkat Mardhani ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic