5

I keep getting number format expectations, even though I'm trimming the strings and they don't contain non numerical characters bizarrely it works for some numbers and not others. Below is an example of a string I get number format exception for. Also, any string starting with 0 e.g "0208405223", is returned 208405223, there's no zero anymore is that supposed to happen?

String n="3020857508";
Integer a = Integer.parseInt(n.trim());
System.out.println(a);

This is the exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "3020857508"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at JavaBeans.Main.main(Main.java:15)
user207421
  • 305,947
  • 44
  • 307
  • 483
manic bubble
  • 147
  • 1
  • 3
  • 13
  • 3
    Because the number goes out of range for an `int`. – Codebender Aug 06 '15 at 04:27
  • 1
    The removed zero is expected; an int only has a value, not a notion of the format, and any leading zeroes would be formatting unrelated to the value. If you need to output it with leading zeroes to a specified width, you'd need to convert it back to a String using [String.format](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29) with an appropriate format code, e.g. `"%010d"` to make the output ten characters wide with zero padding if the value requires less than 10 characters to display. – ShadowRanger Aug 06 '15 at 05:02
  • yes thanks, I realise my mistake now. – manic bubble Aug 06 '15 at 13:17

8 Answers8

9

The largest number parseable as an int is 2147483647 (231-1), and the largest long is 9223372036854775807 (263-1), only about twice as long.

To parse arbitrarily long numbers, use:

import java.math.BigInteger;

BigInteger number = new BigInteger(str);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
8

It's because 3020857508 exceeds Integer.MAX_VALUE. You should use long to convert the string to number.

java> String n="3020857508";
//=> java.lang.String n = "3020857508"
java> Integer a = Integer.parseInt(n.trim());
//=> java.lang.NumberFormatException: For input string: "3020857508"
java> Integer.MAX_VALUE
//=> java.lang.Integer res2 = 2147483647
java> Long a = Long.parseLong(n.trim());
//=>java.lang.Long a = 3020857508

The above is javarepl output.

If you are using JDK9 or above, you can see the same result in jshell.

jshell> String n="3020857508"
n ==> "3020857508"

jshell> Integer a = Integer.parseInt(n.trim())
|  Exception java.lang.NumberFormatException: For input string: "3020857508"
|        at NumberFormatException.forInputString (NumberFormatException.java:65)
|        at Integer.parseInt (Integer.java:652)
|        at Integer.parseInt (Integer.java:770)
|        at (#2:1)

jshell> Integer.MAX_VALUE
$3 ==> 2147483647

jshell> Long a = Long.parseLong(n.trim());
a ==> 3020857508
ntalbs
  • 28,700
  • 8
  • 66
  • 83
1

MAX_INT is 2147483647 and you're trying to parse a bigger number as Integer.

You can use Long.parseLong instead:

System.out.println(Long.parseLong("3020857508")); // 3020857508
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

The number you're trying to parse is larger than the max value of an Integer, you would have to use a larger data type like Long.

public static void main(String[] args) {
    System.out.println("Integer max value: " + Integer.MAX_VALUE);
    System.out.println("Long max value: " + Long.MAX_VALUE);
    System.out.println();

    String n = "3020857508";
    Long a = Long.parseLong(n.trim());
    System.out.println(a);
}

Results:

Integer max value: 2147483647
Long max value: 9223372036854775807

3020857508
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

You could use

String n="3020857508";
Long a = Long.valueOf(n.trim()).longValue();
System.out.println(a);
Harshit
  • 5,147
  • 9
  • 46
  • 93
0

Just like what @Codebender said. Your value is out of range for int. Try using long/Long instead.

jtothebee
  • 165
  • 8
0
    String n="3020857508";
    Long l = Long.parseLong(n.trim());
    System.out.println(l);

Integer MAX_VALUE : 2147483647, Long MAX_VALUE : 9223372036854775807

As string n will not fit in Integer after conversion we have to use Long.

And if you are still not sure about range that can come in String. go with BigIntegerwhere number is held in an int[]

swapy
  • 1,616
  • 1
  • 15
  • 31
0

In Java, the integer range is from -2,147,483,648 to 2,147,483,647 make sure your numeric value is between the same.

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39