Postfix'StringIndexOutOfBoundsException'的错误

时间:2019-03-28 22:08:09

标签: java indexoutofboundsexception postfix-notation infix-notation

我正在设置一个方法,该方法使用自定义LinkStack将中缀字符串转换为后缀方程。

我试图检查charAt(i)是否为null和if语句来检查i是否大于exp.length(),但均无效。

public static String infixToPostfix(String exp) 
    { 
        // make variable
        String result = new String(""); 
        int temp = 0;
        LinkedStack stack = new LinkedStack();

        for (int i = 0; i<exp.length(); ++i) 
        { 
            char c = exp.charAt(i); 
            if(Character.isDigit(c)) 
            { 
                int n = 0; 

                //extract the characters and store it in num 
                while(Character.isDigit(c)) 
                { 
                    n = n*10 + (int)(c-'0'); 
                    i++;
                    c = exp.charAt(i); //exception occurs
                    System.out.println(n);
                } 
                i--; 

                //push the number in stack 
                stack.push(n); 
                //System.out.println(stack.size() + ", Stack size");
            } 

            // If ( push it to the stack. 
            if (c == '(') 
                stack.push(c); 

            //  If ) pop and output from the stack  
            // until an '(' is encountered. 
            else if (c == ')') 
            { 
                while (!stack.isEmpty() && stack.peek() != '(') 
                    result += stack.pop(); 

                if (!stack.isEmpty() && stack.peek() != '(') 
                    return "Invalid Expression"; // invalid expression                 
                else
                    stack.pop(); 
            } 
            else // an operator is encountered 
            { 
                while (!stack.isEmpty() && pre(c) <= pre((char) stack.peek()))
                    result += stack.pop(); 
                stack.push(c); 
            } 

        } 

        // pop all the operators from the stack 
        while (!stack.isEmpty()) 
            result += stack.pop(); 

        String temp2 = stack.print();
        System.out.println(temp2);
        return result; 
    }

如果输入为496 + 645,但实际输出为java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:7,我希望输出为469 645 +。

1 个答案:

答案 0 :(得分:0)

            while(Character.isDigit(c)) 
            { 
                n = n*10 + (int)(c-'0'); 
                i++;
                c = exp.charAt(i); //exception occurs
                System.out.println(n);
            } 

您无需在此处进行长度检查,因此您可以轻松地从字符串末尾开始解析。

            while(i < exp.length() && Character.isDigit(c)) 
            { 
                n = n*10 + (int)(c-'0'); 
                if (++i < exp.length()) {
                    c = exp.charAt(i); //exception occurs
                }
                System.out.println(n);
            } 

注意:由于要使用该长度,我会缓存该长度,但这不是造成问题的原因。

但是请注意,这是更简洁的代码样式:

public class Foo {
    public static void main(String[] args) {
        String myString = "12345";
        int index = 0;
        for (char c: myString.toCharArray()) {
            System.out.printf("Char at %d == %c\n", index, c);
            ++index;
        }
    }
}

注意for循环。我没有做您的计算或分解或其他任何事情,但这是一种更干净的方法。

您也可以...

for (int index = 0; index < exp.length(); ++index) {
    char c = exp.charAt(index);
    if (!Character.isDigit(c)) {
        break;
    }
    // Do other stuff here.
}

还有多种其他方式来构造代码。您的while循环很尴尬。