import java.util.Scanner; public class PostFixCalculator { public static void main(String [] args) { Scanner kbd = new Scanner(System.in); int result; String expression; System.out.println("Student name, CS-304, Fall 2014, Asst 2c."); System.out.println("To quit this program, just hit 'return'.\n"); System.out.print("Enter a postfix expression: "); expression = kbd.nextLine(); while (!expression.equals("")) { try { } catch(RuntimeException e) { } System.out.print("\nEnter a postfix expression: "); expression = kbd.nextLine(); } // end while (!expression.equals("")) System.out.println("\nBye!"); } // end public static void main(String [] args) public static int postFixEvaluate(String input) { Scanner tokenizer; int result, operand1, operand2, value; String operator; LinkedStack s = new LinkedStack(); tokenizer = new Scanner(input); while (tokenizer.hasNext()) { if (tokenizer.hasNextInt()) { value = tokenizer.nextInt(); s.push(value); } else // we have an operator { operator = tokenizer.next(); if (s.isEmpty()) throw new RuntimeException ("Not Enough Operands"); operand2 = s.pop(); if (s.isEmpty()) throw new RuntimeException ("Not Enough Operands"); operand1 = s.pop(); if (operator.equals("+")) result = operand1 + operand2; else if (operator.equals("-")) result = operand1 - operand2; else if (operator.equals("*")) result = operand1 * operand2; else if (operator.equals("/")) result = operand1 / operand2; else throw new RuntimeException ("Not Enough Operands"); s.push(result); } // end else // we have an operator } // end while (tokenizer.hasNext()) if (s.isEmpty()) throw new RuntimeException ("Not Enough Operands"); result = s.pop(); if (!s.isEmpty()) throw new RuntimeException ("Not Enough Operands"); return result; } // end public static int postFixEvaluate(String input) } // end public class PostFixCalculator
I have this code, PostFixCalculator, but everything that I inserted at this time and trying, I got an error all the time. The program compiles and starts as it is placed, but it does not work correctly. I hit the wall.
source share