Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文本文件中读取会产生ArrayIndexOutOfBoundsException_Java_File Io_Postfix Notation - Fatal编程技术网

Java 在文本文件中读取会产生ArrayIndexOutOfBoundsException

Java 在文本文件中读取会产生ArrayIndexOutOfBoundsException,java,file-io,postfix-notation,Java,File Io,Postfix Notation,我试图将这个.txt文件读入我的程序(作为对手动输入的改进),但在转换方法以接受输入的txt文件时遇到了问题。我在“中缀[--pos]='\0';行上获得arrayindexoutofboundsexception 类函数{ 无效后缀(字符中缀[],字符后置[]){ int位置,und=1; 整数输出=0; char-topsymb='+'; 字符符号; Stack opstk=新堆栈(); opstk.top=-1; 对于(position=0;(symb=infix[position])!=

我试图将这个.txt文件读入我的程序(作为对手动输入的改进),但在转换方法以接受输入的txt文件时遇到了问题。我在“中缀[--pos]='\0';行上获得arrayindexoutofboundsexception

类函数{
无效后缀(字符中缀[],字符后置[]){
int位置,und=1;
整数输出=0;
char-topsymb='+';
字符符号;
Stack opstk=新堆栈();
opstk.top=-1;
对于(position=0;(symb=infix[position])!='\0';position++){
if(等标量(符号))
post[outposition++]=symb;
否则{
if(opstk.isempty()==1)
und=1;
否则{
und=0;
topsymb=opstk.pop();
}
while(und==0&&priority(topsymb,symb)==1){
post[outposition++]=topsymb;
if(opstk.isempty()==1)
und=1;
否则{
und=0;
topsymb=opstk.pop();
}
}//结束时
如果(und==0)
opstk.push(topsymb);
如果(und==1 | |(symb!='))
opstk.push(symb);
其他的
topsymb=opstk.pop();
}//结束其他
}//结束
while(opstk.isempty()==0)
post[outposition++]=opstk.pop();
post[outposition]='\0';
}//结束后缀函数
int优先级(char-topsymb,char-symb){
/*检查优先级并返回0或1*/
如果(topsymb=='(')
返回0;
如果(symb=='(')
返回0;
如果(symb==')')
返回1;
if(topsymb=='$'&&symb=='$')
返回0;
if(topsymb=='$'和&symb!='$'))
返回1;
if(topsymb!='$'和&symb=='$'))
返回0;
如果((topsymb=='*'| | topsymb==='/')和&(symb!='$'))
返回1;
如果((topsymb='+'| | topsymb=='-')和&(symb='-'| | symb=='+'))
返回1;
如果((topsymb='+'| | topsymb=='-')和&(symb='*'| | symb=='/'))
返回0;
返回1;
}/*结束优先函数*/
专用布尔等标量(字符符号){
/*如果符号是数字,则返回1,否则返回0*/

如果(symb>='0'&&symbDo不应该这样做:

try {
...
} catch (IOException e) {
}
您丢失了一些有关代码运行的基本信息

至少您应该打印堆栈跟踪以跟踪调查:

e.printStackTrace();
您可能有FileNotFound异常

此外,在
中缀[--pos]
中尝试将数组索引为-1,在此语句之前,pos设置为0。

1)完全放在一边,但我认为main中的行应该是:System.out.println(“\n输入中缀形式的表达式:”); 2) 同样,我同意catch声明。你已经把它缩小为一个ioexcation,但是你可以通过在catch中打印以下内容来找到更多的信息 System.err.println(e.getMessage())或e.printStackTrace()

现在回答您的问题。您正在将pos初始化为值0,但是您正在中缀[--pos]='\0'行上进行预增量;因此pos变为-1(显然超出了数组边界的范围)

我想你想把它改成增量后的中缀[pos-->='\0';。也许吧


是的,您的代码看起来确实像C…

pos
到达该行时是-1。您的代码中使用的是什么
pos
?这个代码看起来像C而不是Java吗?我建议您在线查看一些开始的Java文档。以这种方式处理数组不是典型的,并且会导致像yo这样的异常你真的应该试着写你自己的代码,让你熟悉它,而不是使用别人的代码,然后在它不起作用的时候寻求帮助。我相信在论坛的帖子末尾,它说“你可以修改”代码。我不是在试图剽窃,只是为了用类似的代码来解决不同的问题我试图从文件中读取,而不是手动输入。
e.printStackTrace();