中缀到Postfix'StringIndexOutOfBoundsException'错误[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

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

我试图检查charAt(i)是否为null和if语句来检查我是否大于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,我希望输出为469 645 +,但实际输出是java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:7。

java indexoutofboundsexception postfix-notation infix-notation
1个回答
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循环很尴尬。

© www.soinside.com 2019 - 2024. All rights reserved.