2016-09-14 68 views
1

我從字符串做簡單的轉換爲int,但得到的數字格式異常:獲取數字格式異常

我有以下Java程序使用:

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

獲取例外如下:

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) 

爲什麼我得到上述異常?

+0

這是超出範圍。 –

+0

int的最大值約爲20億。 70億太大了。 – khelwood

+0

它因爲值大於整數範圍int:32位 整數值範圍從-2147483648開始到2147483647 –

回答

1

這是因爲它超出了整數的範圍。 integer的最大允許值爲2147483647

在Java中,以下是最小值和最大值。

 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


使用 '長' 數據類型,而不是

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