Java Assignment Chapter *12.1 (NumberFormatException) Listing 7.9, Calculator.ja
ID: 3861540 • Letter: J
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 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
Explanation / Answer
A Java application or program can accept any number of arguments from the command line. The arguments can be passed by specifying them after the name of the class[Ref: Oracle Docs]. Consider Calculator.java as example.
1. The class is compiled by command: javac Calculator.java
2. Then it is executed by command: java Calculator
To pass arguments, the user can specify following command:
java Calculator 1 + 156
When a program is executed, the runtime system passes the command-line arguments to the program's main method via an array of Strings( parameter args as array of Strings here). Note that the space character separates command-line arguments. [Ref: Oracle Docs]
Here args[0] contains 1, args[1] contains + and args[2] contains 156.
When the program executes, operator will be stored as args[1] which is '+' and the first switch case will be executed.
Note: '*' is a wildcard character in shell. Therefore to pass * argument for multiplication, you need to escape it like "*" or /*.
Further to check whether operand is non-numeric without using exception handlers, we can convert the passed string argument into array of characters and then check whether each character is less than '0' or greater than '9'.
Please find the modified Calculator.java program below:
class Calculator{
public static void main(String[] args) {
for(int i=0; i<args.length; i++){
System.out.println(" "+i+" Argument:"+args[i]);
}
if(args.length != 3){
System.out.println("Command Usage: java Calculator Operand1 Operator Operand2 ");
System.exit(0); //to exit from the program
}
if(!isNumeric(args[0])){
System.out.println("First Argument is non-numeric.");
System.exit(0); //to exit from the program
}
if(!isNumeric(args[2])){
System.out.println("Second Argument is non-numeric.");
System.exit(0); //to exit from the program
}
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");
}
}
public static boolean isNumeric(String argument){
for (char ch: argument.toCharArray()){
if (ch < '0' || c > '9') return false;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.