0

我正在尝试制作一个计算器应用程序,但不幸的是我在解析字符串时遇到了困难。

应用程序接受输入并将其显示在EditText et2. 当按下“等于”键时,计算器会执行以下工作。

et2 = (EditText) findViewById(R.id.editText2);
s = et2.getText().toString();
int k = 0, i = 0, j = 0, operatorTop = -1, operandTop = -1, end = 0;
float left, right;
float operandStack[] = new float[50];
char operatorStack[] = new char[50];

for (i = 0; i < s.length(); i++) {

    if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*'
            || s.charAt(i) == '/') {

        if (i == 0 || i == s.length() - 1) {
            break;
        } else {

            left = Float.valueOf(s.substring(k, i - 1));
            operandStack[++operandTop] = left;

            operatorStack[++operatorTop] = s.charAt(i);
            if (operatorStack[operatorTop] == '*'
                    || operatorStack[operatorTop] == '/') {
                for (j = i + 1; j < s.length(); j++) {
                    if (s.charAt(j) == '+' || s.charAt(j) == '-'
                            || s.charAt(j) == '*' || s.charAt(j) == '/')
                        end = j - 1;
                    if (j == s.length() - 1)
                        end = j;

                }
                right = Float.valueOf(s.substring(i + 1, end));
                if (operatorStack[operatorTop] == '*')
                    operandStack[operandTop] = operandStack[operandTop]
                            * right;
                if (operatorStack[operatorTop] == '/')
                    operandStack[operandTop] = operandStack[operandTop]
                            / right;
                operatorTop--;

            }
            k = i++;

        }
    }
}
operandStack[++operandTop] = Float.valueOf(s.substring(k,
        s.length() - 1));
for (i = operatorTop; i > -1; i--) {
    char ch = operatorStack[operatorTop];
    switch (ch) {
    case '+':
        operandStack[operandTop - 1] += operandStack[operandTop];
        operandTop--;
        operatorTop--;
        break;
    case '-':
        operandStack[operandTop - 1] += operandStack[operandTop];
        operandTop--;
        operatorTop--;
        break;

    }
    et1.setText("" + operandStack[operandTop]);
}

我收到 NumberFormatException 错误。这是错误日志:

03-09 16:54:07.493: E/AndroidRuntime(362): FATAL EXCEPTION: main
03-09 16:54:07.493: E/AndroidRuntime(362): java.lang.IllegalStateException: Could not       execute method of the activity
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.view.View$1.onClick(View.java:2072)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.view.View.performClick(View.java:2408)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.view.View$PerformClick.run(View.java:8816)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.os.Handler.handleCallback(Handler.java:587)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:521)
03-09 16:54:07.493: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-09 16:54:07.493: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-09 16:54:07.493: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)
03-09 16:54:07.493: E/AndroidRuntime(362): Caused by: java.lang.reflect.InvocationTargetException
03-09 16:54:07.493: E/AndroidRuntime(362):  at yadav.sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:309)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:521)
03-09 16:54:07.493: E/AndroidRuntime(362):  at android.view.View$1.onClick(View.java:2067)
03-09 16:54:07.493: E/AndroidRuntime(362):  ... 11 more
03-09 16:54:07.493: E/AndroidRuntime(362): Caused by: java.lang.NumberFormatException: 
03-09 16:54:07.493: E/AndroidRuntime(362):  at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:305)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.Float.parseFloat(Float.java:291)
03-09 16:54:07.493: E/AndroidRuntime(362):  at java.lang.Float.valueOf(Float.java:330)
03-09 16:54:07.493: E/AndroidRuntime(362):  ... 15 more
4

2 回答 2

1
Caused by: java.lang.NumberFormatException

所以问题是您的String格式无效,无法解析为数字。所以我的建议是在执行解析之前使用例如正则表达式:

if (yourString.matches("-?[0-9]+")) { // or "[+-]?\\d*(\\.\\d+)?"
   // do your work.
}

注意:还添加 try-catch 块来捕获Exception并制作一些关于问题的日志(例如打印字符串值)。

于 2013-03-09T11:58:29.607 回答
1

您正在尝试将一个无整数值解析为 Integer

Caused by: java.lang.NumberFormatException:

按照 logcat 中提供的行号更正您的错误

你的错误一定发生在这里

 left=Float.valueOf(s.substring(k,i-1));

其他在这里

 right=Float.valueOf(s.substring(i+1,end));

尝试这样做

String leftString=s.substring(k,i-1);

并检查是否leftString可以转换为float/Integer

于 2013-03-09T11:50:27.903 回答