Hi, before anyone trashes my question, I have not been taught this yet so forgive my ignorance. I would like to correctly handle the exception for user input of 1-4 when someone inputs a letter instead. This is my code so far. It all works except the handling part( I know its not right, but do not know how to do it right, yet.) Any help in understanding how to do this would be greatly appreciated.
import java.util.Scanner; import java.io.Console; class WeatherProgram{ public static void main(String[]args){ double fahrenheit = 0; double celsius = 0; double chill = 0; double reply; String reply2 = "y"; Console c = System.console(); Scanner scan = new Scanner(System.in); while(reply2.equals("y")){ try { System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n"); System.out.println("1. Fahrenheit to Celsius"); System.out.println("2. Celsius to Fahrenheit"); System.out.println("3. Windchill"); System.out.println("4. Quit"+"\n"); reply = scan.nextDouble(); } catch (java.util.InputMismatchException){ } if (reply != (1-4)){ System.out.println("I'm sorry, that is not a chioce. Please choose 1-4: "+"\n"); System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n"); System.out.println("1. Fahrenheit to Celsius"); System.out.println("2. Celsius to Fahrenheit"); System.out.println("3. Windchill"); System.out.println("4. Quit"+"\n"); reply = scan.nextDouble(); } if (reply == 1){ fToC(fahrenheit); } if (reply == 2){ cToF(celsius); } if (reply == 3){ tToWC(chill); } reply2 = c.readLine("Do you wish to continue Weather Conversion, (y/n)?: "); } System.out.println("Thank you"); } static double fToC(double fahrenheit){ double celsius; Scanner scan = new Scanner(System.in); System.out.println("Please input fahrenheit temperature: "); fahrenheit = scan.nextDouble(); celsius = ((fahrenheit-32)*5/9); if (celsius<1){ System.out.println(fahrenheit + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!"); } if (celsius>32){ System.out.println(fahrenheit + " Fahrenheit = " + celsius + " Celsius. It is hot!"); } if((celsius>1) && (celsius<32)){ System.out.println(fahrenheit + " Fahrenheit = " + celsius + " Celsius."); } return celsius; } static double cToF(double celsius){ double fahrenheit; Scanner scan = new Scanner(System.in); System.out.println("Please input celsius temperature: "); celsius = scan.nextDouble(); fahrenheit = ((celsius*9/5)+32); if (fahrenheit<33){ System.out.println(celsius + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!"); } if (fahrenheit>90){ System.out.println(celsius + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!"); } if((fahrenheit>33) && (fahrenheit<90)){ System.out.println(celsius + " Celsius = " + fahrenheit + " Fahrenheit."); } return fahrenheit; } static double tToWC(double chill){ double temperature; double windSpeed; Scanner scan = new Scanner(System.in); System.out.println("Please input temperature in degrees fahrenheit: "); temperature = scan.nextDouble(); System.out.println("Please input wind speed in MPH: "); windSpeed = scan.nextDouble(); chill = (35.74 + (0.6215*temperature) - (35.75*Math.pow(windSpeed,0.16)) + (.4275*temperature*Math.pow(windSpeed,0.16))); if (chill<-30){ System.out.println("Windchill = " + chill + " Degrees Fahrenheit. Dangerous Windchill!"); } else{ System.out.println("Windchill = " + chill + " Degrees Fahrenheit"); } return chill; } }