Q:

(NumberFormatException) Listing 7.9, Calculator.java, is a simple commandline calculator

0

(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).

FIGURE 12.12 The program performs arithmetic operations and detects input errors.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class CalculatorWithException {
	/** Main method */
	public static void main(String[] args) throws NumberFormatException {
		// Check number of strings passed
		if (args.length != 3) {
			System.out.println(
				"Usage: java Calculator operand1 operator operand2");
			System.exit(0);
		}

		// The result of the operation
		int result = 0;

		// Exeception handler to deal with nonnumeric operands
		try {
			// Determine the operator
			switch (args[1].charAt(0)) {
				case '+' : result = Integer.parseInt(args[0]) +
										  Integer.parseInt(args[2]);
							  break;
				case '-' : result = Integer.parseInt(args[0]) -
										  Integer.parseInt(args[2]);
							  break;
				case '.' : result = Integer.parseInt(args[0]) *
										  Integer.parseInt(args[2]);
							  break;
				case '/' : result = Integer.parseInt(args[0]) /
										  Integer.parseInt(args[2]);
							  break;
			}	

			// Display result
			System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
				+ " = " + result);
		} 
		catch (NumberFormatException ex) {
			System.out.println("Wrong Input: " + 
				ex.getMessage().substring(ex.getMessage().indexOf(""") + 1, 
					ex.getMessage().lastIndexOf(""")));
		}
	}
}
public class CalculatorWithoutException {
	/** Main method */
	public static void main(String[] args) throws Exception {
		// Check number of strings passed
		if (args.length != 3) {
			System.out.println(
				"Usage: java Calculator operand1 operator operand2");
			System.exit(0);
		}
		
		// Test for nonnumeric operands
		testDigit(args[0]);
		testDigit(args[2]);

		// The result of the operation
		int result = 0;

		// Determine the operator
		switch (args[1].charAt(0)) {
			case '+' : result = Integer.parseInt(args[0]) +
									  Integer.parseInt(args[2]);
						  break;
			case '-' : result = Integer.parseInt(args[0]) -
									  Integer.parseInt(args[2]);
						  break;
			case '.' : result = Integer.parseInt(args[0]) *
									  Integer.parseInt(args[2]);
						  break;
			case '/' : result = Integer.parseInt(args[0]) /
									  Integer.parseInt(args[2]);
						  break;
		}

		// Display result
		System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
			+ " = " + result);
	}

	// Deals with nonnumeric operands
	private static void testDigit(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				System.out.println("Wrong Input: " + str);
				System.exit(0);
			}
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now