/**
 * A simple calculator.
 *
 * This version illustrates how try-catch blocks can be nested.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 2.0
 */
public class CalculatorNested
{
    /**
     * The entry point of the application.
     *
     * @param args  Syntax: left-operand operator right operand
     */
    public static void main(String[] args)
    {
       char           operator;
       int            left, result, right;

       result = 0;
       
       try
       {
           left  = Integer.parseInt(args[0]);
           try
           {
               right = Integer.parseInt(args[2]);
               operator = args[1].charAt(0);

               try
               {
                   switch (operator) 
                   {
                       case '+': 
                           result = left + right;
                           break;
                           
                       case '-': 
                           result = left - right;
                           break;
                           
                       case '/': 
                           result = left / right;
                           break;
                   }
                   System.out.println("Result: "+result);
               } 
               catch (ArithmeticException ae) 
               {
                   System.out.println("Does not compute!");
               } 
           }
           catch (NumberFormatException rnfe)
           {
               System.out.println("Right-side operand is non-numeric!");
           }
       }
       catch (NumberFormatException lnfe) 
       {
           System.out.println("Left-side operand is non-numeric!");
       }
    }
}
