G Hat G Hat - 7 months ago 31
Java Question

Java Postfix Expression Evaluator

I really need help with my post-fix expression calculator. I don't really know what's wrong with the code that I've written, however when I run the program, it just prints whatever number is on the top. For example, if I input "7 2 +", the output is 2. If I input "2 7 +", the output is 7. Could somebody please point me in the right direction on how to fix this? I think (not sure) that the problem is that my program is not able to properly detect the operands "+" and "*". However, I can't tell why.

File #1:

import java.io.*;
import java.util.*;

public class ProblemTwo {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("Enter a post value expression: ");
String input = scan.nextLine();
StringTokenizer st = new StringTokenizer(input);
Stack hello = new Stack(st.countTokens());

for (int i = 0; i <= st.countTokens(); i++) {
String inputToken = st.nextToken();
if (inputToken.trim().contains("+")) {
int sum = Integer.parseInt(hello.pop() + Integer.parseInt(hello.pop()));
System.out.println(sum);
hello.push(Integer.toString(sum));
}
else if (inputToken.trim().contains("*")){
int product = Integer.parseInt(hello.pop()) * Integer.parseInt(hello.pop());
hello.push(Integer.toString(product));
}
else {
hello.push(inputToken);
}
}
System.out.println(hello.pop());
}
}


File #2:

public class Stack {

private String[] stackArray;
private int arraySize;
private int top;

public Stack(int capacity) {
arraySize = capacity;
stackArray = new String[arraySize];
top = -1;
}

public void push(String i) {
stackArray[++top] = i;
}

public String pop() {
return stackArray[top--];
}

public boolean isEmpty() {
return top == -1;
}

public boolean isFull() {
return top == arraySize - 1;
}
}

Answer

Your code has at least 2 problems: Your took not all inputed value in for loop and your stack has broken implementation. Here is working version.

File #1:

import java.io.*;
import java.util.*;

public class ProblemTwo {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a post value expression: ");
        String input = scan.nextLine();
        StringTokenizer st = new StringTokenizer(input);
        Stack hello = new Stack(st.countTokens());

        for (int i = 0; i <= st.countTokens()+1; i++) {
            String inputToken = st.nextToken();
            if (inputToken.trim().contains("+")) {
                int sum = Integer.parseInt(hello.pop()) + Integer.parseInt(hello.pop());
                System.out.println(sum);
                hello.push(Integer.toString(sum));
            }
            else if (inputToken.trim().contains("*")){
                int product = Integer.parseInt(hello.pop()) * Integer.parseInt(hello.pop());
                hello.push(Integer.toString(product));
            }
            else {
                hello.push(inputToken);
            }
        }
        System.out.println(hello.pop());
    }
} 

File #2:

public class Stack {

    private String[] stackArray;
    private int arraySize;
    private int top;

    public Stack(int capacity) {
        arraySize = capacity;
        stackArray = new String[arraySize];
        top = -1;
    }

    public void push(String i) {
        stackArray[++top] = i;
    }

    public String pop() {
        return stackArray[top--];
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public boolean isFull() {
        return top == arraySize - 1;
    }
}
Comments