我正在使用此函数来查明String是否为数字
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;
}
目的是用户可以引入各种类型的投影。每当它们不是数字或整数数字时,程序将再次要求引入数字,但不是现在发生的CRASH。有什么想法吗?
答案 0 :(得分:2)
当您将逗号分隔的字符串文字转换为整数时,它会在下面的行中崩溃。
number1=Integer.parseInt(str);
你可能想要这样做。
number1 = Integer.parseInt(str.replaceAll(",", ""));
答案 1 :(得分:2)
可能有一种更简单的方法来处理您的问题。您可以尝试直接将输入解析为double,然后确保没有抛出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确定什么是有效的字符串数字,而不是使用正则表达式来猜测自己。
如果您必须继续使用当前的方法,请继续阅读。您的代码有问题,因为它验证了 double ,而不是整数,但您试图通过以下方法将验证的输入解析为整数:
number1 = Integer.parseInt(str);
相反,如果数字通过验证,请使用此代码解析为double:
NumberFormat format = NumberFormat.getInstance(new Locale("es", "ES"));
Number number = format.parse(str);
double d = number.doubleValue();
用于验证的正则表达式也存在问题,因为+98,8
之类的数字不能正确解析为double(尽管-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
如果你想允许前导加号,那么修改正则表达式,但请注意,在将字符串解析为double之前你必须将其删除。
答案 2 :(得分:0)
以下行投掷错误
number1=Integer.parseInt(str);
答案 3 :(得分:0)
新解决方案:
public static boolean esNumero (String s)
{
try
{
Integer.parseInt(s);
return true;
}
catch (NumberFormatException e){return false;}
}
答案 4 :(得分:0)
Integer.parseInt()不支持在某些国家/地区用于分隔数字的逗号。而是可以使用DecimalFormat,它允许这样做。
NumberFormat format = new DecimalFormat();
System.out.println(format.parse("12,34").intValue());
输出
8899
注意:DecimalFormat依赖于Locale,它发生在我当前的位置,然后我的Local是en_GB,这意味着默认情况下groupingUsed标志是打开的。你可以通过调用...强制它。
format.setGroupingUsed(true);
当输入无效时,NumberFormat实例thow ParseException,因此这可用于验证您的号码
try {
System.out.println(format.parse("88,99").intValue());
} catch (ParseException e) {
// do something else
}