• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do no know why intstream fails to parse this string, any help . please.




it returnNumberFormatException



 
Diego Nobrega
Greenhorn
Posts: 3
Java Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you were trying to get a number very very bigger than int type maximum capabilitie.
I dont know if im mistake, but i guess the maximum positive value for a int type is around 2 billion approximately.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a much bigger possible number, declare it long instead of int.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diego Nobrega, welcome to the Ranch
You are correct; you can see the limits for the primitive integer types here. What OP didn't tell us is whether this is an exercise in interconversion between different number bases. If you convert an int to a String representing its binary digits, you will end up with a String much longer than the decimal representation. So 4095₁₀ turns into 11111111111₂. Your four digits have become eleven digits. When you pass that to Integer#parseInt, that method is programmed to interpret that in decimal, so you get the Exception. You can use a similar method which takes the radix as an argument; with a radix of 2, “11111111111” is a valid argument to that method. If you are trying to count the 1 bits in that String, the parseInt method isn't what you want at all; something like this would work better.
There are ready‑made methods in the Integer class which look for 1 and 0 bits, but I don't think there is one to count consecutive runs of bits.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!