2017-12-03 313 views
0

一個字符串轉換爲一個整數值,當我試圖解析字符串輸入1到一個整數值它崛起的一個NumberFormatException.I've試圖如有替換字符串中的空間,但事實並非如此爲我工作。NumberFormatException異常而在Java

int number = 0; 
String input1 = "12345354987"; 
try{ 
    ip = Integer.parseInt(input1); 

    }catch(NumberFormatException e){ 
     System.out.println("not a number"); 
    } 
+0

您是否嘗試過尋找爲電子商務的堆棧跟蹤?例外通常包含非常有用的信息。 –

+1

@Eran 謝謝,它的工作對我來說..! –

+0

見https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE。 MAX_VALUE = 2_147_483_647 –

回答

0
12345354987 > 2,147,483,64 

整數最大範圍是2,147,483,647和你穿越它。對於大數字,您可以使用Long或BigDecimal。

0

它,因爲在字符串的數字超出整數的範圍的,如果你真的需要這比使用BigInteger的大型值是這樣的:

String input1 = "12345354"; 
     BigInteger ib =new BigInteger(input1); 
     System.out.println(ib); 

或 對於能夠適應遠距離就可以較小的值使用:

String input1 = "1234535455"; 
      Long ip = Long.parseLong(input1); 
      System.out.println(ip); 
0

您的號碼大於整數最大值請使用long然後嘗試解析。