2017-03-10 56 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 
}