0

ok, I have a program that uses a list and stack to convert Infix to Postfix. I finally worked out all the kinks with it except one thing, which I'll get into in a minute, and now I want to reverse the process, turn Postfix into Infix. I gave it a go, and the output was all wrong. I tried debugging it and I have no idea where it's messing up. So I have 2 problems

1)If I make a MyTest class like:

public class MyTest{

    public static void main(String args[]){

        Postfix px = new Postfix();

        String input = "(a1+a2*a123)+123";
        px.parsing(input);
        px.conversion();
        px.clear();

        input = "a*b+c*d*e+f";
        px.parsing(input);
        px.conversion();
        px.clear();
    }
}

It will output the first part correctly, but when it gets to the second input it throws:
java.lang.StringIndexOutOfBoundsException
right after running the parsing method, which I don't understand why it would do that.

2) When I run the MyTest class without the second input but with the reverse method in there it prints out a weird output that looks like this:
( a123 + ) )( ( ( + a1 ) * a2 )( ( a1 + ( a2 * a123 ) ) + 123 ) when it should be
( ( a1 + ( a2 * a123 ) ) + 123 )
I walked through the debug and I don't see how it could be doing this because it looked ok to me, but then again I am also new to debugging.

I'll put up the code to my Postfix class below. Any help you guys could offer would be greatly appreciated.

class Postfix {

    private ListReferenceBaseCSCE1040 token = new ListReferenceBaseCSCE1040();
    private String output;
    private StackReferenceBaseCSCE1040 aStack = new StackReferenceBaseCSCE1040();

    public Postfix(){

        token = new ListReferenceBaseCSCE1040();
        aStack = new StackReferenceBaseCSCE1040();
        output = "";

    }//end constructor postfix

    //this takes token and converts it to postfix
    public void conversion(){

        for(int i = 1; i <= token.size(); i++){

            if( token.get(i).equals("(")){
                aStack.push(token.get(i));
            }else if(token.get(i).equals(")")){
                while(!aStack.peek().equals("(")){
                    output =  output + " " + aStack.pop() + " "; 
                } //end while
                aStack.pop(); // to pop the ( so it doesn't get printed later
            }else if((token.get(i).equals("*")) || (token.get(i).equals("/")) || (token.get(i).equals("+")) || (token.get(i).equals("-"))){
                while(!aStack.isEmpty() && precedence(token.get(i)) <= precedence(aStack.peek())){
                    output = (output + " " + aStack.pop());
                } //end while
                aStack.push(token.get(i));
            }else{
                output += token.get(i) + " ";
            } //end ifs

        } //end for

        while(!aStack.isEmpty()){
            output = output + aStack.pop();
        } // end while

        System.out.println("Output: " + output);
        System.out.println("");

    } //end conversion

    // sets the precedence of the operators
    public int precedence(Object op){

        if((op.equals("*")) || (op.equals("/"))){
            return 2;
        }else if((op.equals("+")) || (op.equals("-"))){
            return 1;
        }else{ //parenthesis
            return 0;
        } //end ifs

    } //end precedence

    public void parsing(String input){

        String aToken = "";
        int numToken = 0;
        int x = 0;

        if(output == ""){
            System.out.println("Infix to Postfix");
        }

        while( x < input.length() ){

            if((input.charAt(x) == '(') || (input.charAt(x) == ')') || (input.charAt(x) == '*') || (input.charAt(x) == '/') || (input.charAt(x) == '+') || (input.charAt(x) == '-')){
                aToken = aToken + input.charAt(x);
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(Character.isLetter(input.charAt(x))){
                aToken += input.charAt(x);
                while((x+1<input.length()) && (Character.isLetter(input.charAt(x+1))) || (Character.isDigit(input.charAt(x+1)))){
                    x++;
                    aToken += input.charAt(x);
                } //end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(Character.isDigit(input.charAt(x))){
                aToken += input.charAt(x);
                while((x+1 < input.length()) && (Character.isDigit(input.charAt(x+1)))){
                    x++;
                    aToken += input.charAt(x);
                } // end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }

            x++; //adds to the main while loop

        } // end while
        System.out.println("Input: " + input);

    } // end parsing


    public void clear(){

            token = new ListReferenceBaseCSCE1040();
            aStack = new StackReferenceBaseCSCE1040();
            output = "";

    }//end clear


//THIS IS WHERE THINGS GET WEIRD********************************************
    public void reverse(){
        String tempout = "";
        Object operand1 = "";
        Object operand2 = "";

        System.out.println("Postfix to Infix");
        this.parsing(output);

        for(int i = 1; i < token.size(); i++){
            if(token.get(i).equals("*") || token.get(i).equals("/") || token.get(i).equals("+") || token.get(i).equals("-")){
                operand2 = aStack.pop();
                operand1 = aStack.pop();
                tempout = ("( " + operand1 + " " + token.get(i)+ " " + operand2 + " )");
                aStack.push(tempout);
                tempout = "";
            }else{
                aStack.push(token.get(i));
            }//end ifs
        }//end for
        while(!aStack.isEmpty()){
            tempout = tempout + aStack.pop();
        }
        System.out.println("Output: " + tempout);
    }//end reverse  
}//end postfix

Edited by mike_2000_17: Fixed formatting

1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by gramatton
0

scratch the second problem. I fixed my reverse method. Main problem was a forgotten <= in the for loop. I still can't figure out why its throwing that exception though so any help with that would be great.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.