this code cant really get it to work tried combining two separate codes into one but I am coming into trouble

18 Views Asked by At

need help with my code my switch statement doesn't work I've tried to change it from int to string but cant figure it out. The line I have trouble with is line 43 that one is the switch statement that I cant figure how to fix. can someone help me please this is very important thank you.

import java.util.Scanner; import java.util.Stack;

public class RPNCalculator
{
private static Stack<Integer> stack;
private static Object evaluator;
private static String again;
private static Scanner keyboard;

 public RPNCalculator()
    {
        stack = new Stack<Integer>(); //creates stack 
    }

 public static void main(String[] args)
    {
        String expression, again;
        int result;

        Scanner keyboard = new Scanner(System.in);

        do
        {  
            RPNCalculator evaluator = new RPNCalculator();
            System.out.println("Enter a valid post-fix expression one token " +
                               "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
            System.out.println("Each token must be an integer or an operator (+,-,*,/)");
            System.out.println();
            System.out.println("Enter 'q' to quit, 'h' for help.");
            
            expression = keyboard.nextLine();

            result = evaluator.evaluate(expression);
            System.out.println();
            System.out.println("That expression equals " + result);
            
            while (true) {
                System.out.print("Enter command: ");
                Scanner scanner;
                Object input = scanner.nextLine();

                switch (  ) {
                    case "m":
                        if (!stack.isEmpty()) {
                            stack.push(-stack.pop());
                        } else {
                            System.out.println("Error: Stack is empty");
                        }
                        break;
                    case "r":
                        if (stack.size() < 2) {
                            System.out.println("Error: Stack does not have enough elements");
                        } else {
                            double a = stack.pop();
                            double b = stack.pop();
                            stack.push((int) a);
                            stack.push((int) b);
                        }
                        break;
                    case "d":
                        if (!stack.isEmpty()) {
                            double top = stack.peek();
                            stack.push((int) top);
                        } else {
                            System.out.println("Error: Stack is empty");
                        }
                        break;
                    case "p":
                        if (!stack.isEmpty()) {
                            System.out.println("Top item: " + stack.peek());
                        } else {
                            System.out.println("Error: Stack is empty");
                        }
                        break;
                    case "n":
                        if (!stack.isEmpty()) {
                            System.out.println("Top item removed: " + stack.pop());
                        } else {
                            System.out.println("Error: Stack is empty");
                        }
                        break;
                    case "f":
                        System.out.println("Stack contents: " + stack);
                        break;
                    case "c":
                        stack.clear();
                        System.out.println("Stack cleared");
                        break;
                    case "q":
                        System.out.println("Exiting program");
                        return;
                    case "h":
                    case "?":
                        System.out.println("Operations:");
                        System.out.println("m - unary minus");
                        System.out.println("r - exchange the top two items");
                        System.out.println("d - duplicate top item on stack");
                        System.out.println("p - print the top item");
                        System.out.println("n - print and remove the top item");
                        System.out.println("f - print all contents of the stack");
                        System.out.println("c - clear the stack");
                        System.out.println("q - quit");
                        System.out.println("h or ? - print help message");
                        break;
                    default:
                        
                        
                }
            
            }

            System.out.print("Evaluate another expression [Y/N]? ");
            again = keyboard.nextLine();
            System.out.println();
        }
        while (again.equalsIgnoreCase("y"));
   }

public int evaluate(String expr) 
    {
        int op1, op2, result = 0;
        String token;
        Scanner parser = new Scanner(expr);     

        while (parser.hasNext())        
        {
            token = parser.next();          

            if (isOperator(token))  //if operator pop 
            {
                op2 = (stack.pop()).intValue();
                op1 = (stack.pop()).intValue();
                result = evaluateSingleOperator(token.charAt(0), op1, op2);     //
                stack.push(new Integer(result));
            }
            else
                stack.push(new Integer(Integer.parseInt(token)));       
        }

        return result;
    }

 private boolean isOperator(String token)
    {
        return ( token.equals("+") || token.equals("-") ||
                 token.equals("*") || token.equals("/") || token.equals("%") );

    }

 private int evaluateSingleOperator(char operation, int op1, int op2)
    {
        int result = 0;

        switch (operation)
        {
            case '+':
                result = op1 + op2;
                break;
            case '-':
                result = op1 - op2;
                break;
            case '*':
                result = op1 * op2;
                break;
            case '/':
                result = op1 / op2;
                break;
            case '%':
                result = op1 % op2;
                break;
        }

        return result;
    }
 

}

1

There are 1 best solutions below

0
GoharSahi On

@Justin, your switch statement is incomplete. switch needs to be provided an expression as a parameter e.g.

switch (input) {

I'm not a Java developer so I can only guide you so you can learn more about switch statement here.

You already seem to have got switch right in your code further below.

switch (operation)