Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: exception handling

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default exception handling

    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;
    	}
    }
    Last edited by Topflyt; December 2nd, 2011 at 05:57 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: exception handling

    if (reply != (1-4)){
    When the expression (1-4) is evaluated, it should give a value of -3 which changes your code to:
    if (reply != -3){

    If you want to compare a number to a range of numbers, check if it is >= the lower number AND that it is <= the higher number.
    >= and <= are binary operators that take two operands.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: exception handling

    Ya, I'm sorry, rewrote the code. Here is the new code. Would still like to error handle the exception possibility of input being a letter and not a number with a try/catch.Please help with try/catch(haven't been taught yet).

     
     
    import java.util.Scanner;
    import java.io.Console;
     
    class WeatherProgram0{
     
    	public static void main(String[]args){
    		double reply1 = 0;
    		mainProgram(reply1);
    	}
     
    	static double mainProgram(double reply1){
    		double fahrenheit = 0;
    		double celsius = 0;
    		double chill = 0;
    		String reply2 = "y";
    		Console c = System.console();
    		Scanner scan = new Scanner(System.in);
     
    		while(reply2.equals("y")){
    			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"+"\n");
    			reply1 = scan.nextDouble();
     
    			if (reply1 == 1){
    				fToC(fahrenheit);
    			}
    			if (reply1 == 2){
    				cToF(celsius);
    			}
    			if (reply1 == 3){
    				tToWC(chill);
    			}
    		System.out.println("");
    		reply2 = c.readLine("Would you like to continue Weather Program, (y/n)?");
    		}
    		System.out.println("Thank you");
    		return(reply1);
    	}
     
    	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;
    	}
    }
    Last edited by Topflyt; December 2nd, 2011 at 09:21 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: exception handling

    You have removed the try catch blocks you had in the first posting.
    Can you write down the logic you want to use with the try{} catch blocks? Where do you want to catch the error and what do you want to do when you do catch an error?
    One way is to use a loop to get input from the user. If there is good input, exit the loop.
    If the input is bad, tell the user and loop back to get the input again. The code stays in the loop until the user enters good input or he tells you he wants to quit the program.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: exception handling

    Thank you Norm, that is what I did. I have already turned that in.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: exception handling

    Good luck.

Similar Threads

  1. Exception handling
    By JavaGirl9001 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 26th, 2011, 08:45 PM
  2. Exception Handling
    By hello_world in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 5th, 2011, 05:47 PM
  3. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM
  4. Exception handling for TextFields
    By FretDancer69 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 16th, 2009, 07:48 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM