我正在尝试制作一个计算器应用程序,但不幸的是我在解析字符串时遇到了困难。
应用程序接受输入并将其显示在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