简体   繁体   English

如何修复NumberFormatException?

[英]How can i fix NumberFormatException?

I know this error is caused by a number format exception but i don't know how to fix it. 我知道此错误是由数字格式异常引起的,但我不知道如何解决。 i'v tried to do a function to Check if the variable is a Number or String and here is my code 我试图做一个函数来检查变量是数字还是字符串,这是我的代码

public boolean checkNumber(String x) {
   try{
       Integer.parseInt(x);
       return true;
   }catch(NumberFormatException ex){
       return false;
   }
}

and this is a function takes a parameter of format dd/mm/yyyy, split and store it in an array, then call the previous function to determine if all index are integer number or not. 这是一个函数,它采用dd / mm / yyyy格式的参数,将其拆分并存储在数组中,然后调用上一个函数来确定所有索引是否均为整数。

public boolean CheckBirthDate(String birthdate){
    int B = 0;
    String Line = birthdate;
    String[] seprated = Line.split("/");
    if( checkNumber(seprated[0])  && checkNumber(seprated[1])  && checkNumber(seprated[2])) {
        B = 1;
    }
    if(seprated.length == 3 && Integer.parseInt(seprated[0]) <= 31  && Integer.parseInt(seprated[1]) <=   12 && Integer.parseInt(seprated[2]) >= 1950 &&  Integer.parseInt(seprated[2]) <= 2017 && B == 1){
        return true;
    }else{
        return false;
    }
}

Why not use a DateFormat? 为什么不使用DateFormat?

String birthday = "25/05/1973";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
try {
    Date date = format.parse(birthday);
    System.out.println("date = " + date);
}
catch (ParseException e) {
     //date was bad format
}

Result: 结果:

date = Fri May 25 00:00:00 CET 1973

This will work: 这将起作用:

public boolean CheckBirthDate(String birthdate) {
    int B = 0;
    String Line = birthdate;
    String[] seprated = Line.split("/");
    if (checkNumber(seprated[0]) && checkNumber(seprated[1]) && checkNumber(seprated[2])) {
        B = 1;
    }
    if (B == 1 && seprated.length == 3 && Integer.parseInt(seprated[0]) <= 31 && Integer.parseInt(seprated[1]) <= 12
        && Integer.parseInt(seprated[2]) >= 1950 && Integer.parseInt(seprated[2]) <= 2017) {
        return true;
    } else {
        return false;
    }
}

The problem is, that you check if B == 1 at the end of the if-clause and not the beginning. 问题是,您在if子句的末尾而不是开头检查B == 1 Therefore java tries to parse the Integer, even though it hasn't verified that it is actually a number yet. 因此,即使Java尚未验证它实际上是一个数字,它也会尝试解析Integer。

I would also use a boolean vor B instead of an int. 我还将使用布尔vor B代替int。 But generally speaking SurfMan's solution is probably the best and prettiest 但总的来说,SurfMan的解决方案可能是最好,最漂亮的

I would recommend to debug in order to figure out where the error ocurs. 我建议进行调试,以找出错误发生的位置。

Any way, the last conditional, I suggest you to change the order. 无论如何,最后一个条件,我建议您更改顺序。 You first check if strings are numbers, thats perfect, then you store the result in a variable, fine. 您首先检查字符串是否为数字,那就是完美的,然后将结果存储在变量中,很好。 The thing is that the way conditionals work could be the problem here. 问题是条件工作的方式可能是这里的问题。 You check whether are numbers or not at the end, and should be done at the begining, like: 您检查数字是否在结尾处,并且应该从头开始,例如:

if (B == 1 && seprated.length == 3 && Integer.parseInt(seprated[0]) <= 31 && Integer.parseInt(seprated[1]) <= 12
        && Integer.parseInt(seprated[2]) >= 1950 && Integer.parseInt(seprated[2]) <= 2017) {
        return true;
    } else {
        return false;
    }

This way first checks the first conditional, and if it is false, it doesnt keep doing the other conditions because false & true is always false 这种方式首先检查第一个条件,如果它为false,则它不会继续执行其他条件,因为false和true始终为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM