Search code examples
javaif-statementvariablesuser-inputcommand-line-arguments

Variable in Java Not Recognized


For an assignment I'm creating a command line program to change an inputted temp from Celsius (C) to Fahrenheit (F) & vice versa. The program runs fine until the user inputs the temp type (C/F), and then it doesn't seem to recognize the user input. What I am doing wrong?

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the temperature:"); //Prompts user for temperature
        String temp = input.nextLine(); //Allows user to input temp data
        double tempDouble = Double.parseDouble(temp); //Changes input from string to double

        System.out.println("Is " + temp + " degrees in Celsius or Fahrenheit? (Enter C or F):"); //Prompts user for type of temp
        String type = input.nextLine(); //Allows user to input temp type

        if (type == "C") { //Checks if temp is Celsius
            double tempF = 0;
            tempF = (tempDouble * 1.8) + 32; //Converts temp to Fahrenheit
            System.out.println(tempDouble + "C equals " + tempF + "F."); //Displays conversion of C to F
            //Tf = Tc * 1.8 + 32

        } else if (type == "F") { //Checks if temp is Fahrenheit
            double tempC = 0;
            tempC = (tempDouble - 32) / 1.8; //Converts temp to Celsius
            System.out.println(tempDouble + "F equals " + tempC + "C.");
            //Tc = (Tf - 32) / 1.8
        }
        System.out.println("Incorrect input for Celsius or Fahrenheit"); //Tells user they didn't input C or F correctly
    }

Solution

  • There are two problems in the code

    1. You are checking object equality not the value of the object. User String.equals method for that.
    2. The construct of the if-else block is wrong, it will always print "Incorrect input for Celsius or Fahrenheit".

    Here is the correct one -

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        System.out.println("Please enter the temperature:"); //Prompts user for temperature
        String temp = input.nextLine(); //Allows user to input temp data
        double tempDouble = Double.parseDouble(temp); //Changes input from string to double
    
        System.out.println("Is " + temp + " degrees in Celsius or Fahrenheit? (Enter C or F):"); //Prompts user for type of temp
        String type = input.nextLine(); //Allows user to input temp type
    
        if ("C".equals(type)) { //Checks if temp is Celsius
            double tempF = 0;
            tempF = (tempDouble * 1.8) + 32; //Converts temp to Fahrenheit
            System.out.println(tempDouble + "C equals " + tempF + "F."); //Displays conversion of C to F
            //Tf = Tc * 1.8 + 32
    
        } else if ("F".equals(type)) { //Checks if temp is Fahrenheit
            double tempC = 0;
            tempC = (tempDouble - 32) / 1.8; //Converts temp to Celsius
            System.out.println(tempDouble + "F equals " + tempC + "C.");
            //Tc = (Tf - 32) / 1.8
        }else{
            System.out.println("Incorrect input for Celsius or Fahrenheit"); //Tells user they didn't input C or F correctly 
        }
    }