0

I'm working on a program that's supposed to take in a mathematical expression and use stacks and stack operations to convert said expression from infix to postfix, i.e, 5 + 9 * 3 would become 5 9 3 * + . I believe most of the code for this part is complete; however, for some reason, every time I try the input provided to us the program just parrots what I feed it. The program I have completed so far is as follows:

    import java.util.Scanner;

public class StackTester {
    public static void main(String[] args)  {
        boolean quit = false;
        int input;
        final String ADD="+", SUB="-", MUL="*", DIV="/";
        do  {
            System.out.println("1. Convert infix to postfix");
            System.out.println("2. Convert postfix to infix");
            System.out.println("3. Exit.");

            Scanner keyboard = new Scanner(System.in);
            input = keyboard.nextInt();

            switch(input)   {
            case 1:
                ArrayStack<String> stack = new ArrayStack<String>();
                System.out.println("Enter an infix expression: ");
                Scanner Input = new Scanner(System.in);
                String expression = Input.nextLine();
                String[] storedExp = expression.split(" ");


                for(int count=storedExp.length-1;count>=0;count--)  {
                    stack.push(storedExp[count]);
                }

                //System.out.println(stack.toString());
                ArrayStack<String> operands = new ArrayStack<String>();
                String temp = "";
                while(!stack.isEmpty()) {
                    if(stack.peek()==ADD||stack.peek()==SUB||stack.peek()==MUL||stack.peek()==DIV&&operands.isEmpty())  {
                        String store = stack.pop();
                        System.out.println(store);
                        operands.push(store);
                    }
                    if(stack.peek()==ADD||stack.peek()==SUB||stack.peek()==MUL||stack.peek()==DIV&&!operands.isEmpty()) {
                        System.out.println(stack.peek());
                        if(operands.peek()==MUL||operands.peek()==DIV&&stack.peek()==MUL||stack.peek()==DIV)    {
                            String store = stack.pop();
                            operands.push(store);
                        }
                        if(operands.peek()==MUL||operands.peek()==DIV&&stack.peek()==ADD||stack.peek()==SUB)    {
                            temp += operands.toString()+" ";
                            String store = stack.pop();
                            operands.push(store);
                        }
                        if(operands.peek()==ADD||operands.peek()==SUB&&stack.peek()==MUL||stack.peek()==DIV)    {
                            String store = stack.pop();
                            operands.push(store);
                        }
                        if(operands.peek()==ADD||operands.peek()==SUB&&stack.peek()==ADD||stack.peek()==SUB)    {
                            String store = stack.pop();
                            operands.push(store);
                        }
                    }
                    /*if(stack.peek()=="("||stack.pop()==")")   {
                        stack.pop();
                    }
                    */
                    else    {
                        temp += stack.pop()+" ";
                    }
                }
                System.out.println(temp);
                if(!operands.isEmpty()) {
                    temp += operands.toString()+" ";

                }
                System.out.println(temp);
                break;
            }

        }

        while(!quit);
    }

}

And for the ArrayStack class:

public class ArrayStack<T> implements StackADT<T>
{   
    private static final int DEFAULT_CAPACITY = 100;
    private int top;
    private T[] stack;
    private String test;

    public ArrayStack() {
        top = 0;
        stack = (T[]) (new Object[DEFAULT_CAPACITY]);
    }
    public void push(T element) {
        for(int count=0;count<stack.length;count++) {
            if(stack[count]==null)  {
                stack[count] = element;
                top = count;
                break;
            }
        }
    }
    public T pop()  {
        T element = stack[top];
        stack[top] = null;
        if(top!=0)  {
            top--;
        }
        return element;
    }
    public T peek() {
        return stack[top];
    }
    public boolean isEmpty()    {
        if(stack[0]==null)
            return true;
        else{
            return false;
        }
    }
    public int size()   {
        int length = 0;
        for(int count=0;count<stack.length;count++) {
            if(stack[count]!=null)  {
                length++;
            }
            else if(stack[count]==null) {
                break;
            }
        }
        return length;
    }
    public String toString()    {
        String array = "";
        for(int count=0;count<stack.length;count++) {
            if(stack[count]!=null)  
                array = array+stack[count]+" ";
        }
        return array;
    }



/*  public static void main(String[] args)  {
        boolean quit = false;
        int input;
        String expression;
        do  {
            System.out.println("1. Convert infix to postfix");
            System.out.println("2. Convert postfix to infix");
            System.out.println("3. Exit.");

            java.util.Scanner keyboard = new java.util.Scanner(System.in);
            input = keyboard.nextInt();

            switch(input)   {
            case 1:
                System.out.println("Enter an infix expression");
                expression = keyboard.next();
                for(int count=0;count<expression.length();count++)  {
                    Character e = expression.charAt(count);
                    push();
                }
                break;
            }
        }
        while(!quit);
    }
*/
}

As far as the output is concerned, the program seems to just spit out whatever I feed it exactly as it was.

Any ideas what I'm doing wrong?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Vincents
  • 43
  • 13
  • 1
    Related: [How do I compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis Sep 26 '13 at 15:10
  • 1
    Oh dear, the silly mistakes you can make when you're not paying attention. Thanks, that helped a lot! It's still not doing the conversion exactly as it should, but I'm getting a much better output now than I was before. Now I just need to get the precedence of the operators working... – Vincents Sep 26 '13 at 15:22

0 Answers0