user2883232
user2883232

Reputation: 13

How to use char from keyboard in if statement [Java]

I am having a bit of a problem with my code. I need to make a temperature conversion from Celsius to Fahrenheit and vice-versa with the user choosing either "F" or "C" (lower or upper case) but cannot seem to figure out how to do it properly. I don't know how to have it recognize that the variable is supposed to be entered via the keyboard.

 Scanner Keyboard = new Scanner(System.in); 
 System.out.println("Type C to convert from Fahrenheit to Celsius or" + 
        "F to convert from Celsius to Fahrenheit."); 
 char choice = Keyboard.nextLine().charAt(0);
 //Get user input on whether to do F to C or C to F 
 if (choice == F) //Fahrenheit to Celsius
 { 
      System.out.println("Please enter the temperature in Fahrenheit:"); 
      double C = Keyboard.nextDouble();
      double SaveC = C;
      C = (((C-32)*5)/9);
      System.out.println(SaveC + " degrees in Fahrenheit is equivalent to " + C + " degrees in Celsius."); 
 } 
 else if (choice == C) 
 { 
      System.out.println("Please enter the temperature in Celsius:"); 
      double F = Keyboard.nextDouble(); 
      double SaveF = F;
      F = (((F*9)/5)+32); 
      System.out.println(SaveF +" degrees in Celsius is equivalent to " + F + " degrees in Fahrenheit."); 
 } 
 else if (choice != C && choice != F) 
 { 
      System.out.println("You've entered an invalid character.");
 } 

Upvotes: 1

Views: 77814

Answers (3)

shajata
shajata

Reputation: 1

import java.util.Scanner;

public class conversion {

    public static void main(String[] args) {
        Scanner Keyboard = new Scanner(System.in); 
        System.out.println("Type C to convert from Fahrenheit to Celsius or" + " " +
        "F to convert from Celsius to Fahrenheit."); 
        char choice = Keyboard.nextLine().charAt(0);
        //Get user input on whether to do F to C or C to F 
        if (choice == 'F' || choice == 'f') //Fahrenheit to Celsius
            { 
                System.out.println("Please enter the temperature in Fahrenheit:"); 
                double C = Keyboard.nextDouble();
                double SaveC = C;
                C = (((C-32)*5)/9);
                System.out.println(SaveC + " degrees in Fahrenheit is equivalent to " + C + " degrees in Celsius."); 
            } 
        else if (choice == 'C' || choice == 'c') 
            { 
                System.out.println("Please enter the temperature in Celsius:"); 
                double F = Keyboard.nextDouble(); 
                double SaveF = F;
                F = (((F*9)/5)+32); 
                System.out.println(SaveF +" degrees in Celsius is equivalent to " + F + " degrees in Fahrenheit."); 
            } 
        else 
            { 
                System.out.println("You've entered an invalid character.");
            } 
    }
    
}

Upvotes: 0

Boann
Boann

Reputation: 50031

When comparing with the choice variable your F and C characters should be wrapped in single quotes to make them character literals. Use || (meaning 'or') to test for upper or lower case. I.e.,

if (choice == 'F' || choice == 'f')
    ...
else if (choice == 'C' || choice == 'c')
    ...
else
    ...

Upvotes: 0

scotch
scotch

Reputation: 475

You can use Scanner to read your input and then call to see if it is equal to "C" or "F"

For example,

Scanner x = new Scanner(System.in);

String choice = x.nextLine();

if (choice.equals("F") || choice.equals("f")) { 
    blah blah blah
}
if (choice.equals("C") || choice.equals("c")) {
    blah blah blah 
}

Upvotes: 1

Related Questions