Question: Java Assignment Chapter *12.1 (NumberFormatException) Listing 7.9, Calculator.java, is a simple commandline calculator. Note that the program terminates if any operand is nonnumeric. Write a
Java Assignment Chapter *12.1 (NumberFormatException) Listing 7.9, Calculator.java, is a simple commandline calculator. Note that the program terminates if any operand is nonnumeric. Write a program with an exception handler that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective. Your program should display a message that informs the user of the wrong operand type before exiting (see Figure 12.12).
In the already given solution an expert had posted previously I noticed that they are using a scanner for the part that wanted a user to write a program without an exception handler. I would like to know how I should handle it without scanners and prompting it from the command line. Not prompting the user for each number and making new scanners. How do you assign variables to the parameters given in the command line?
I am trying to used this code
class Calculator{ public static void main(String[] args) { double operand1 = Double.parseDouble(args[0]); double operand2 = Double.parseDouble(args[2]); String operator = args[1]; switch (operator){ case "+": System.out.println("Result = "+(operand1 + operand2)); break; case "-": System.out.println("Result = "+(operand1 - operand2)); break; case "*": System.out.println("Result = "+(operand1 * operand2)); break; case "/": System.out.println("Result = "+(operand1 / operand2)); break; case "%": System.out.println("Result = "+(operand1 % operand2)); break; default: System.out.println("Invalid operation"); } } }
getting error = Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
