2016-09-14 12 views
1

I einfache Konvertierung von String täte int aber immer Zahlenformat Ausnahme:Erste Zahlenformat Ausnahme

Ich habe den Einsatz unter Java-Programm:

String cId = "7000000141"; 
int iCid = Integer.parseInt(cId); 
System.out.println(iCid); 

unter Ausnahme Anfahrt:

Exception in thread "main" java.lang.NumberFormatException: For input string: "7000000141" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
    at java.lang.Integer.parseInt(Integer.java:459) 
    at java.lang.Integer.parseInt(Integer.java:497) 

Warum bekomme ich die obige Ausnahme?

+0

Es außer Reichweite ist. –

+0

Der maximale Wert eines int beträgt etwa 2 Milliarden. 7 Milliarden sind zu groß. – khelwood

+0

Es ist wegen des Werts mehr als Ganzzahlbereich int: 32 Bit für Ganzzahl der Wertebereich beginnt von -2147483648 zu 2147483647 –

Antwort

1

Das liegt daran, dass es außerhalb des Bereichs der Ganzzahl liegt. Der maximal zulässige Wert für integer ist 2147483647

In Java nach der Minimal- und Maximalwerte sind.

 width      minimum       maximum 
int: 32 bit    -2 147 483 648     +2 147 483 647 
long: 64 bit -9 223 372 036 854 775 808  +9 223 372 036 854 775 807 

source


Verwenden Sie eine 'long' Datentyp statt

public class HelloWorld 
{ 
    public static void main(String[] args) 
    { 
    String cId = "7000000141"; 
    long iCid = Long.parseLong(cId); 
    System.out.println(iCid); 
    } 
}