读入文本文件会产生ArrayIndexOutOfBoundsException

时间:2012-09-27 16:36:08

标签: java file-io postfix-notation

我正在尝试将此.txt文件读入我的程序(作为对手动输入的改进),并且我无法将我的方法转换为接受输入txt文件。我得到一个arrayindexoutofboundsexception行“infix [ - pos] ='\ 0';”

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]);

    }
}

2 个答案:

答案 0 :(得分:0)

绝不应该这样做:

try {
...
} catch (IOException e) {
}

您丢失了有关代码运行的一些重要信息。

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

e.printStackTrace();

您可能有一个FileNotFound异常。

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

答案 1 :(得分:0)

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

现在回答你的问题。你正在将pos初始化为0,但是你正在对行中缀[ - pos] ='\ 0'进行PREINCREMENT;所以pos变为-1(显然超出了数组边界的范围)。

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

是的,你的代码看起来像C ......

相关问题