[英]Reading in text file gives ArrayIndexOutOfBoundsException
我正在尝试将此.txt文件读入我的程序(作为对手动输入的改进),并且在转换方法以接受输入txt文件时遇到麻烦。 我在行“ infix [-pos] ='\\ 0';”上得到arrayindexoutofboundsexception
class Functions {
void postfix(char infix[], char post[]) {
int position, und = 1;
int outposition = 0;
char topsymb = '+';
char symb;
Stack opstk = new Stack();
opstk.top = -1;
for (position = 0; (symb = infix[position]) != '\0'; position++) {
if (isoperand(symb))
post[outposition++] = symb;
else {
if (opstk.isempty() == 1)
und = 1;
else {
und = 0;
topsymb = opstk.pop();
}
while (und == 0 && precedence(topsymb, symb) == 1) {
post[outposition++] = topsymb;
if (opstk.isempty() == 1)
und = 1;
else {
und = 0;
topsymb = opstk.pop();
}
}// end while
if (und == 0)
opstk.push(topsymb);
if (und == 1 || (symb != ')'))
opstk.push(symb);
else
topsymb = opstk.pop();
}// end else
}// end for
while (opstk.isempty() == 0)
post[outposition++] = opstk.pop();
post[outposition] = '\0';
}// end postfix function
int precedence(char topsymb, char symb) {
/* check precedence and return 0 or 1 */
if (topsymb == '(')
return 0;
if (symb == '(')
return 0;
if (symb == ')')
return 1;
if (topsymb == '$' && symb == '$')
return 0;
if (topsymb == '$' && symb != '$')
return 1;
if (topsymb != '$' && symb == '$')
return 0;
if ((topsymb == '*' || topsymb == '/') && (symb != '$'))
return 1;
if ((topsymb == '+' || topsymb == '-') && (symb == '-' || symb == '+'))
return 1;
if ((topsymb == '+' || topsymb == '-') && (symb == '*' || symb == '/'))
return 0;
return 1;
} /* end precedence function */
private boolean isoperand(char symb) {
/* Return 1 if symbol is digit and 0 otherwise */
if (symb >= '0' && symb <= '9')
return true;
else
return false;
}/* end isoperand function */
}
public class Driver {
public static void main(String[] args) throws IOException {
Functions f = new Functions();
char infix[] = new char[80];
char post[] = new char[80];
int pos = 0;
char c;
System.out.println("\nEnter an expression is infix form : ");
try {
BufferedReader in = new BufferedReader(new FileReader("infix.txt"));
String str;
while ((str = in.readLine()) != null) {
infix = str.toCharArray();
}
in.close();
} catch (IOException e) {
}
infix[--pos] = '\0';
System.out.println("The original infix expression is : ");
for (int i = 0; i < pos; i++)
System.out.print(infix[i]);
f.postfix(infix, post);
System.out.println("\nThe postfix expression is : ");
for (int i = 0; post[i] != '\0'; i++)
System.out.println(post[i]);
}
}
不要做这样的事:
try {
...
} catch (IOException e) {
}
您失去了一些有关代码运行的基本信息。
至少应打印堆栈跟踪以进行调查:
e.printStackTrace();
您可能有FileNotFound异常。
另外,您尝试在infix[--pos]
中将数组索引为-1,在此语句之前pos设置为0。
1)完全放在一边,但我认为main中的行应显示为:System.out.println(“ \\ n以infix形式输入表达式:”); 2)我也同意catch声明。 您已经将其范围缩小为IOExcpetion,但可以通过在catch System.err.println(e.getMessage())内打印以下内容来找到更多信息。 或e.printStackTrace()
现在回答您的问题。 您正在将pos初始化为值0,但是您正在对infix [-pos] ='\\ 0';行进行PREINCREMENT; 因此pos变为-1(显然不在数组边界的范围内)。
我认为您想将其更改为帖子增量infix [pos--] ='\\ 0';。 也许?
是的,您的代码看起来像C ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.