2017-03-10 55 views
2

我使用這個功能,找出一個字符串是否是數字找出一個字符串是否是數字的函數拋出

public static boolean esNumerico(String s) { 
    return s.matches("[-+]?\\d*\\,?\\d+"); 
    } 

程序啓動時的例外,它要求用戶引進了一批。 如果我介紹了數88,99它崩潰了:

Exception in thread "main" java.lang.NumberFormatException: For input string: "98,8" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at actividad03.Ejercicio01.pideEntero(Ejercicio01.java:32) 
at actividad03.Ejercicio01.main(Ejercicio01.java:14) 

完整的功能代碼爲:

static int pideEntero(){ 
    int number1; 
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 
    String str = null; 
    try{ 
     do { 
      System.out.println("Introduzca un número entero:"); 
       str = br.readLine(); //Toma una línea y la almacena como String 
     } while (esNumerico(str)==false); 
    } 

    catch(Exception e) 
    {System.out.println("Dato no válido."); 
    } 

    number1=Integer.parseInt(str); 
    System.out.println("Valor numérico correcto!"); 
    //number1=Integer.parseInt(str); 
    return number1; 
    } 

目的是使用戶可以引入各種樣imputs的。只要它們不是數字或整數數字,程序就會再次要求引入數字,但不會像現在這樣發生。任何想法?

+0

跟蹤告訴你它在parseInt函數崩潰,因爲它期望一個int。要麼嘗試接聽電話,要麼確保您的號碼是一個整數。 –

+0

解決方案是實現另一個功能,以確定Sting的數量與否。通過這種方式:公共靜態布爾esNumero(String s)將 \t \t { \t \t嘗試\t \t \t { \t \t \t的Integer.parseInt(S); \t \t \t return true; \t \t \t} 趕上(NumberFormatException的五){返回false;} } – heptagono

+0

'98,8'是* *可能數字,但它是不** **的整數 –

回答

2

由於您不能將逗號分隔的字符串文字轉換爲Integer,因此它會在下面的行中崩潰。

number1=Integer.parseInt(str); 

你可能想這樣做,而不是。

number1 = Integer.parseInt(str.replaceAll(",", "")); 
2

可能有更簡單的方法來處理您的問題。您可以嘗試直接將輸入解析爲雙精度,然後確保沒有引發NumberFormatException。事情是這樣的:

double number1; 
do { 
    System.out.println("Introduzca un número entero:"); 
    try { 
     str = br.readLine(); 
     number1 = Double.parseDouble(str); 
     // with a valid input in hand, we can now break and leave the loop 
     break; 
    } catch (NumberFormatException e) { 
     System.out.println("El numero es inválido, introduzca otra vez:"); 
    } catch (Exception e) { 
     System.out.println("Otro problema, introduzca otra vez:"); 
    } 
} while (esNumerico(str) == false); 

這可能是真正的路要走,因爲我們讓Java的決定是什麼,是不是有效的串號,而不是試圖用一個正則表達式來猜測自己。

如果您必須繼續使用您當前的方法,請繼續閱讀。您的代碼有問題,因爲它驗證了,而不是一個整數,而你正試圖解析通過此驗證的輸入爲整數

number1 = Integer.parseInt(str); 

相反,如果該號碼通過驗證,使用該代碼解析爲雙:

NumberFormat format = NumberFormat.getInstance(new Locale("es", "ES")); 
Number number = format.parse(str); 
double d = number.doubleValue(); 

,還設有用於驗證您正則表達式的一個問題,因爲一個數字,如+98,8將無法​​正確解析爲雙(儘管-99,8會)。嘗試使用這個:

public static boolean esNumerico(String s) { 
    return s.matches("[-]?\\d+(,\\d+)?"); 
} 

正則表達式的說明:

[-]?  match an optional leading minus sign 
\\d+  followed by one or more digits 
(,\\d+)? followed by an optional comma, and then one or more digits 

如果你想允許前導的加號,然後修改正則表達式,但請注意,你將不得不解析之前將其刪除把字符串變成雙精度。

0

以下行拋出錯誤

number1=Integer.parseInt(str); 
0

一個新的解決方案:

public static boolean esNumero (String s) 

    { 

    try 

     { 

     Integer.parseInt(s); 

     return true;  

     } 


    catch (NumberFormatException e){return false;} 

    } 
0

的Integer.parseInt()不支持用於在一些國家分離的數字逗號。相反,可以使用DecimalFormat,它允許這樣做。

NumberFormat format = new DecimalFormat(); 
System.out.println(format.parse("12,34").intValue()); 

輸出

8899 

注:DecimalFormat的是依賴於語言環境,它發生在我的當前位置,然後我的地方是EN_GB這意味着groupingUsed標誌默認是打開的。你可以通過調用強制...

format.setGroupingUsed(true); 

的NumberFormat實例thow ParseException的時候輸入無效,所以這可以用來驗證也是你的號碼

try { 
    System.out.println(format.parse("88,99").intValue()); 
} catch (ParseException e) { 
    // do something else 
}