import java.util.Scanner; public class IntegerCalculator { /** * Drives the RPN (postfix) calculator for integers. The user can enter * a decimal integer, which gets passed to the calculator as an operand, * the operations "+", "-" and "*", which also get passed to the calculator, * "p", which prints the stack (with the current value on the top), or * "q", which quits the calculator * */ public static void main (String [] args) { Scanner scan = new Scanner (System.in); RPNCalculator calc = new RPNCalculator (); for (;;) { if (scan.hasNextInt()) // If the next input is an operand calc.operand(new MyInt(scan.nextInt())); else { String s = scan.next(); // The three operators if (s.equals("+") || s.equals("-") || s.equals("*")) calc.operation(s); else if (s.equals("p")) // Print calc.print(); else if (s.equals("q")) { // Quit System.out.println("Bye!"); return; } else // Inform the user of proper input format System.out.println("Valid inputs are integers, and symbols +, -, *, p, and q."); } } } }