我正在尝试做一个允许将中缀转换为后缀快递的应用程序。我只能对一位数执行此操作,但是当我对整个实数执行此操作时,它会显示错误!
public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.displayStack("For " + ch + " ");
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
if (input.charAt(j + 1) != '+' || input.charAt(j + 1) != '-'
|| input.charAt(j + 1) != '/'
|| input.charAt(j + 1) != '('
|| input.charAt(j + 1) != ')')
number = number + ch;
else
output = output + number;
break;
}
}
while (!theStack.isEmpty()) {
theStack.displayStack("While ");
output = output + theStack.pop();
}
theStack.displayStack("End ");
return output;
}
这就是错误!
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
at java.lang.String.charAt(String.java:658)
at InToPost.doTrans(InToPost.java:33)
at InfixApp.main(InfixApp.java:15)
if (input.charAt(j + 1) != '+' || input.charAt(j + 1) != '-'...
整个程序很长,这里不能发!
最后编辑
default:
if (j != input.length() - 1) {
if (input.charAt(j + 1) != '+'
|| input.charAt(j + 1) != '-'
|| input.charAt(j + 1) != '/'
|| input.charAt(j + 1) != '('
|| input.charAt(j + 1) != ')')
number = number + ch;
else
output = output + number;
} else
output = output + number;
break;