I have a line that looks like
if(numb2 < 10000000000000 & numb2 > 100000000000){
So in Eclipse it says 10000000000000 and 100000000000 are both out of the integer literal range. Specifically
The literal 10000000000000 of type int is out of range and The literal 1000000000000 of type int is out of range
I changed the line so it looked like
if(numb2 < 1000000000*10000 & numb2 > 100000000*1000){
but if you typed a number in that range it just said
Exception in thread "main" java.lang.NumberFormatException: For input string: "5555555555555"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at twothousandthirteen.LuckyNumber.main(LuckyNumber.java:12)
I would like to know if there is a way to extend the literal number range or do anything to fix the problem.
Thanks KMehta
Those numbers are larger than the largest int
value, which is 231-1 or 2147483647, and which is available as the constant Integer.MAX_VALUE
.
The reason you were able to code it as 1000000000*10000
is because although each multiplicand is within the range of the maximum int
value, the result is not and java handles this by overflowing the result so it falls within the valid range.
To fix the problem, make your variables long
(64 bits) and your constants long also by appending L
at the end if the number (the default numerical type in java is int
), and your should use Long.parseLong()
to cater for the larger values in your input.
Note that, like int
, the range of long
is limited too: to 263-1 or 9223372036854775807, which is available as the constant Long.MAX_VALUE
.
For arbitrarily large numbers, use the BigInteger
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With