简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:“”解析字符串表达式

[英]java.lang.NumberFormatException: For input string: “” parsing string expression

I'm trying to parse a string expression (ie "6+10*2-5") and calculate the answer as a double using a for-case statement. 我试图解析一个字符串表达式(即“ 6 + 10 * 2-5”),并使用for-case语句将答案计算为两倍。 However I'm getting the error: 但是我得到了错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Calculator.random(Calculator.java:50)
    at Calculator.main(Calculator.java:210)

any ideas? 有任何想法吗?

public static double random(String exp) {
    double primeResult = 0;
    for (int i=0; i<exp.length();i++) {
        double left = Integer.parseInt(exp.substring(0,i));
        double right = Integer.parseInt(exp.substring(i+1,i+2));
        switch(exp.charAt(i)) {
            case '*':
            primeResult = left * right;
            break;
        case '/':
            primeResult = left / right;
            break;
        case '+':
            primeResult = left + right;
            break;
        case '-':
            primeResult = left - right;
            break;
        default:
            return 0;
        }
        System.out.println(primeResult);
        }
        return primeResult;
    }
}

Your problem has nothing to do with switch / case . 您的问题与switch / case 无关 The problem is simply that you're trying to parse an empty string as a number. 问题很简单,您正在尝试将空字符串解析为数字。 Work out why you've got an empty string, then fix it. 弄清楚为什么会有一个空字符串,然后修复它。 (Hint: what do you expect exp.substring(0, 0) to return?) (提示:您期望exp.substring(0, 0)返回什么?)

You should also consider what you expect exp.substring(i+1,i+2) to return when i is exp.length() - 1 (or even exp.length() - 2 ). 您还应该考虑当iexp.length() - 1 (甚至是exp.length() - 2 )时exp.substring(i+1,i+2)期望exp.substring(i+1,i+2)返回的内容。

Also, take a step back and consider why you thought this was a problem with the switch / case part. 另外,请退后一步,考虑一下为什么您认为这是switch / case部分的问题。 Look at the stack trace - it's not even within the switch / case statement, but before it. 查看堆栈跟踪-它甚至不在switch / case语句中,而是在它之前。 It's very important to be able to diagnose where errors are actually occurring, so you can focus on that and not get distracted by irrelevant parts. 能够诊断实际发生错误的位置非常重要,因此您可以专注于此,而不会被无关紧要的部分所干扰。

Jon Skeet has answered your question with very good diagnostic training. 乔恩·斯基特(Jon Skeet)通过出色的诊断培训回答了您的问题。

But... I do think your solution to your problem will not work. 但是...我确实认为您无法解决问题。

If you mentally trace through your code, assuming a proper input of "5+5", the first thing your loop will do is this: 如果您在思维上跟踪代码,并假设正确输入“ 5 + 5”,则循环将要做的第一件事是:

double left = Integer.parseInt(exp.substring(0,i));
double right = Integer.parseInt(exp.substring(i+1,i+2));

With i==0 (first time through the loop), this will translate to: 当i == 0(第一次通过循环)时,这将转换为:

double left = Integer.parseInt(exp.substring(0,0));
double right = Integer.parseInt(exp.substring(0+1,0+2));

This will IMMEDIATELY get an "empty string" for exp.substring(0,0) , which throws the exception in your question. 这将立即为exp.substring(0,0)获得一个“空字符串”,这将在您的问题中引发异常。

Oh, and why is your function called "random" if you're evaluating an expression? 哦,如果要计算一个表达式,为什么将您的函数称为“随机”?

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

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