0

I am setting up a method that turn a infix string into a postfix equation with a custom LinkStack.

I have tried to to check if the charAt(i) was null and a if statement to check if i is greater than exp.length() but neither worked.

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; 
    }

I expect the output to be 469 645 + if the input is 496+645 but the actual output is java.lang.StringIndexOutOfBoundsException: String index out of range: 7.

L0cus
  • 1
  • 6

1 Answers1

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

You aren't length checking here, so you readily parse right off the end of the string.

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

Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.

Note, however, that this is cleaner code style:

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;
        }
    }
}

Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.

You can also do...

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

There are a variety of other ways to structure your code. Your while loop is awkward.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36