Search code examples
javaswitch-statementcasejava.util.scannerdefault

Java - default option does not fuction in switch


hello I wrote a program to convert one temperature from one to another using switch.

the default does not fuction:

When i enter a valid option it works perfectly fine, but when i choose invalid choice it goes the default outputting the lines: You enter invalid choice, please choose again and stop workings without allowing me to choose another choice.

earlier when I tested it, the default allowed me to choose another option.

import java.util.Scanner;

public class Tempature {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        double fahrenheit,celcius,kelvin;
        System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
        String word = scan.nextLine();
        switch(word){
            case "f": System.out.println("Enter  Fahrenheit temperature: ");
            fahrenheit=scan.nextDouble();
            celcius = (fahrenheit-32) * 5/9;
            kelvin = (fahrenheit + 459.67) * 5/9;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            case "c": System.out.println("Enter the Celcius temperature: ");
            celcius=scan.nextDouble();
            fahrenheit = (celcius*9)/5 + 32;
            kelvin = celcius + 273.15;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            case "k": System.out.println("Enter the Kelvin temperature: ");
            kelvin=scan.nextDouble();
            fahrenheit = 1.8*(kelvin - 273.15) + 32;
            celcius = kelvin - 273.15;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            default: System.out.println("You enter invalid choice, please choose again");
        }
        scan.close();
    }
}

Solution

  • This could help you.
    add a while loop and read the input of user in default and it will work
    . When user puts q he/she will exit the program.

    class Test {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            double fahrenheit,celcius,kelvin;
            // changed
            System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
            String word = scan.next();
            
            //added
            while(!word.equals("q")) {
                
                switch(word){
                    case "f": {
                        System.out.println("Enter  Fahrenheit temperature: ");
                        fahrenheit=scan.nextDouble();
                        celcius = (fahrenheit-32) * 5/9;
                        kelvin = (fahrenheit + 459.67) * 5/9;
                        System.out.println("" + celcius + " C");
                        System.out.println("" + fahrenheit + " F");
                        System.out.println("" + kelvin + " K");
                        break;
                    }
                    case "c":{
                        System.out.println("Enter the Celcius temperature: ");
                        celcius=scan.nextDouble();
                        fahrenheit = (celcius*9)/5 + 32;
                        kelvin = celcius + 273.15;
                        System.out.println("" + celcius + " C");
                        System.out.println("" + fahrenheit + " F");
                        System.out.println("" + kelvin + " K");
                        break;
                    }
                    case "k": {
                        System.out.println("Enter the Kelvin temperature: ");
                        kelvin=scan.nextDouble();
                        fahrenheit = 1.8*(kelvin - 273.15) + 32;
                        celcius = kelvin - 273.15;
                        System.out.println("" + celcius + " C");
                        System.out.println("" + fahrenheit + " F");
                        System.out.println("" + kelvin + " K");
                        break;
                    }
                    default: {
                        System.out.println("You enter invalid choice, please choose again");
                        // added
                        System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit");
                        word = scan.next();
                        break;
                        }
                    }// end of swtich
                // checking for exit
                if(!word.equals("q")) {
                    System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
                    word = scan.next();
                }
            }// end of while
            System.out.println("Exited");
                }
        }
    

    Output:

    Choose type of temperature:
    f. Fahrenheit
    c. Celcius
    k. Kelvin
    q. Quit
    f
    Enter  Fahrenheit temperature: 
    45
    7.222222222222222 C
    45.0 F
    280.3722222222222 K
    Choose type of temperature:
    f. Fahrenheit
    c. Celcius
    k. Kelvin
    q. Quit
    c
    Enter the Celcius temperature: 
    45
    45.0 C
    113.0 F
    318.15 K
    Choose type of temperature:
    f. Fahrenheit
    c. Celcius
    k. Kelvin
    q. Quit
    k
    Enter the Kelvin temperature: 
    45
    -228.14999999999998 C
    -378.66999999999996 F
    45.0 K
    Choose type of temperature:
    f. Fahrenheit
    c. Celcius
    k. Kelvin
    q. Quit
    cdvd
    You enter invalid choice, please choose again
    Choose type of temperature:
    f. Fahrenheit
    c. Celcius
    k. Kelvin
    q.Quit
    q
    Exited