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 4 of 4

Thread: Default does not work in Switch Statement

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Default does not work in Switch Statement

    The following is a program that I wrote which converts Celsius Temperature to Fahrenheit and vice versa. Everything in the program works EXCEPT the default for the switch statement.

    If the user were to press any number except 1 or 2, then the default statement should notify the user to re-enter the correct number. Instead, the program just skips the default statement and re-prints "Please enter 1 for Celsius and 2 for Fahrenheit. To exit the program, enter 0."

    Here is how the program output looks when I run it:

    /*
    Which temperature would you like to convert?

    Please enter 1 for Celsius and 2 for Fahrenheit. To exit the program, enter 0.

    1.Celsius
    2.Fahrenheit

    Your entry: 1

    Please enter Celsius temperature: 34

    The temperature you entered, 34.0 degrees in Celsius, is 93.20 degrees in Fahrenheit.

    Please enter 1 for Celsius and 2 for Fahrenheit. To exit the program, enter 0.

    1.Celsius
    2.Fahrenheit

    Your entry: 5


    Please enter 1 for Celsius and 2 for Fahrenheit. To exit the program, enter 0. Here is where the program should print: "In-correct degree type entered. Choose 1 or 2 please," but it does not print.

    1.Celsius
    2.Fahrenheit

    Your entry: 0

    Program is closed.

    */

    Here is my coding:

    import java.util.Scanner;
    public class Temperature
    {
    	public static void main(String [] args)
    	{
    		Scanner input = new Scanner(System.in);//create object from Scanner class named input
     
    		int counter = 0;
    		int degreetype = 0;
     
    		System.out.print("Which temperature would you like to convert?");
     
    		do
    		{
     
    			System.out.print("\n\nPlease enter 1 for Celsius and 2 for Fahrenheit. To exit the program, enter 0. \n\n1.Celsius \n2.Fahrenheit");
    			System.out.print("\n\nYour entry: ");
    			degreetype = input.nextInt();
     
    			if (degreetype<=2 && degreetype!=0)
    			{
    				switch(degreetype)
    				{
    				case 1:
    					double celsius = 0.0;
    					System.out.print("\nPlease enter Celsius temperature: ");
    					celsius = input.nextDouble();
    					tempCel(celsius);
    					break;
    				case 2:
    					double fahrenheit = 0.0;
    					System.out.print("\nPlease enter Fahrenheit temperature: ");
    					fahrenheit = input.nextDouble();
    					tempFar(fahrenheit);
    					break;
     
    				default: System.out.println("\nIn-correct degree type entered. Choose 1 or 2 please.");
    				break;[B]//This line does not print if user enters any number other than 1 or 2. I do not know why it does not print. [/B]
    				}
    			}
    			}
    		while(degreetype!=0);			
    		System.out.println("\nProgram is closed.");
    	}
     
    	public static void tempFar(double fahrenheit)
    	{
    		double celsius = 0.00;
    		celsius = 5.0 / 9.0 * (fahrenheit - 32);
    		System.out.printf("\nThe temperature you entered, "+ fahrenheit +" degrees in Fahereheit, is %.2f degrees in Celsius.", celsius);
    	}
    	public static void tempCel(double celsius) 
    	{
    		double fahrenheit = 0.00;
    		fahrenheit = 9.0/5.0 * celsius + 32;
    		System.out.printf("\nThe temperature you entered, "+ celsius +" degrees in Celsius, is %.2f degrees in Fahrenheit.", fahrenheit);
    	}
    }//close class


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Default does not work in Switch Statement

    Quote Originally Posted by StrugglerWithJava View Post
    if (degreetype<=2 && degreetype!=0)
    Your value of '5' is never getting to the switch statement.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default does not work in Switch Statement

    Quote Originally Posted by Chris.Brown.SPE View Post
    Your value of '5' is never getting to the switch statement.
    Hey. I really appreciate your help. It means a lot. I finally got my program to work!

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Default does not work in Switch Statement

    Welcome to programming. Your life will be plagued with this kind of thing. I suggest getting an IDE like Eclipse that allows you to do step by step debugging. Learning how to use this kind of tool is critical! It will make your life so much easier in the future.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  4. Default system look and feel does not work
    By goodguy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 11:46 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM