我正在尝试构建一个没有任何与我尝试解决的操作本身相关的运算符的计算器(比较运算符和带有运算符的循环很好)。问题是,Eclipse 在控制台中显示了这个:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Calculator.addOne(Calculator.java:22)
at Calculator.add(Calculator.java:78)
at Program.main(Program.java:8)
这是我的代码 - 计算器类: http: //pastebin.com/jLGe6atB
我的 main() 方法:
public class Program {
public static void main(String[] args) {
Calculator calculator1 = new Calculator(8135, 6);
calculator1.add();
}
}
有人可以告诉我有什么问题吗?(请不要问我为什么这样做,只是为了好玩)
感谢并为您的时间感到抱歉。
请求:“请在此处发布 add() 和 addOne():
public int addOne(int DigitArray)
{
String stringNum1 = Integer.toString(DigitArray);
String[] arrNum1 = stringNum1.split("");
int[] newArrNum1 = new int[arrNum1.length];
for (int i = 0; i < arrNum1.length; i++) {
newArrNum1[i] = Integer.parseInt(arrNum1[i]);
}
int lastDigit = newArrNum1[newArrNum1.length-1];
switch(lastDigit)
{
case 0:
lastDigit = 1;
System.out.println(lastDigit);
return lastDigit;
case 1:
lastDigit = 2;
System.out.println(lastDigit);
return lastDigit;
case 2:
lastDigit = 3;
System.out.println(lastDigit);
return lastDigit;
case 3:
lastDigit = 4;
System.out.println(lastDigit);
return lastDigit;
case 4:
lastDigit = 5;
System.out.println(lastDigit);
return lastDigit;
case 5:
lastDigit = 6;
System.out.println(lastDigit);
return lastDigit;
case 6:
lastDigit = 7;
System.out.println(lastDigit);
return lastDigit;
case 7:
lastDigit = 8;
System.out.println(lastDigit);
return lastDigit;
case 8:
lastDigit = 9;
System.out.println(lastDigit);
return lastDigit;
case 9:
lastDigit = 0;
updateNumberPlus(lastDigit, arrNum1, newArrNum1);
System.out.println(lastDigit);
return lastDigit;
default:
return lastDigit;
}
}
public int add()
{
int i = 0;
while(i <= num2)
{
i++;
this.addOne(num1);
if(i == num2)
{
result = 0;
System.out.print(result);
return result;
}
}
return result;
}