I'm not familiar with Exception Handling and I don't know where to start. Im suppose to write a program with an exception handler that deals with non-numeric operands; then write another program without using an exception handler to achieve the same objective. My program should display a message that informs the user of the wrong operand type before exiting.
Here's what I got so far:
[CODE=java]public class Calculator {
/** Main method */
public static void main(String[] args) {
// Check command-line arguments
if (args.length != 3) {
System.out.prin tln(
" Please Use java operand1 operator operand2");
System.exit(0);
}
// The result of the operation
int result = 0;
// Determine the operator
switch (args[1].charAt(0)) {
case '+': result = Integer.parseIn t(args[0]) +
Integer.parseIn t(args[2]);
break;
case '-': result = Integer.parseIn t(args[0]) -
Integer.parseIn t(args[2]);
break;
case '*': result = Integer.parseIn t(args[0]) *
Integer.parseIn t(args[2]);
break;
case '/': result = Integer.parseIn t(args[0]) /
Integer.parseIn t(args[2]);
}
// Display result
System.out.prin tln(args[0] + ' ' + args[1] + ' ' + args[2]
+ " = " + result);
}
}[/CODE]
Here's what I got so far:
[CODE=java]public class Calculator {
/** Main method */
public static void main(String[] args) {
// Check command-line arguments
if (args.length != 3) {
System.out.prin tln(
" Please Use java operand1 operator operand2");
System.exit(0);
}
// The result of the operation
int result = 0;
// Determine the operator
switch (args[1].charAt(0)) {
case '+': result = Integer.parseIn t(args[0]) +
Integer.parseIn t(args[2]);
break;
case '-': result = Integer.parseIn t(args[0]) -
Integer.parseIn t(args[2]);
break;
case '*': result = Integer.parseIn t(args[0]) *
Integer.parseIn t(args[2]);
break;
case '/': result = Integer.parseIn t(args[0]) /
Integer.parseIn t(args[2]);
}
// Display result
System.out.prin tln(args[0] + ' ' + args[1] + ' ' + args[2]
+ " = " + result);
}
}[/CODE]
Comment