Monday, April 28, 2014

try/catch problem
































































































































































































































































I want the program to give an error message after the first input if none of the required operators are inputted. But my try catch isn't working? Also when exit is inputted it still as operator the 2nd and 3rd questions ("Enter 1stnum & Enter 2ndnum" are both asked instead of exitting immediately. Is this more of a design issue or can it be overcome? Any comments would be welcomed.
































































































































































































































































import java.util.Scanner;































































































































































































































































import java.io.*;
































































































































































































































































class Monday {































































































































































































































































public static void main(String[] args) {































































































































































































































































double n1,n2;































































































































































































































































boolean check = true;
































































































































































































































































while(check) {































































































































































































































































System.out.println("Enter your operation: add, subtract, divide, multiply, or exit");































































































































































































































































try{































































































































































































































































Scanner myScan = new Scanner(System.in);
































































































































































































































































String op = myScan.next();
































































































































































































































































System.out.println("Enter your 1st number");































































































































































































































































n1 = myScan.nextDouble();































































































































































































































































System.out.println("Enter your 2nd number");































































































































































































































































n2 = myScan.nextDouble();
































































































































































































































































switch (op) {































































































































































































































































case"add":































































































































































































































































System.out.println("Your answer is "+ (n1 + n2));































































































































































































































































break;
































































































































































































































































case"subtract":































































































































































































































































System.out.println("Your answer is "+ (n1 - n2));































































































































































































































































break;
































































































































































































































































case"divide":































































































































































































































































System.out.println("Your answer is "+ (n1 / n2));































































































































































































































































break;
































































































































































































































































case"multiply":































































































































































































































































System.out.println("Your answer is "+ (n1 * n2)) ;































































































































































































































































break;
































































































































































































































































case"exit":































































































































































































































































System.out.println("Goodbye!");































































































































































































































































break;
































































































































































































































































}
































































































































































































































































if ("exit".equals(op))































































































































































































































































check = false;
































































































































































































































































} catch (Exception e) {































































































































































































































































System.out.println("This is my error");
































































































































































































































































}































































































































































































































































}































































































































































































































































}































































































































































































































































}































































































































































































































































































































































































































































































































No comments:

Post a Comment