-1
package temperatureconversion;
import java.util.Scanner;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter Conversion type: Press C for Celsius to Fahrenheit or press F     For Fahrenheit to Celsius.");

 String Vctype = keyboard.next();

    if (Vctype == "f" || Vctype == "F"){
        System.out.println("Please enter fahrenheit");
        double Vfahrenheit = keyboard.nextInt();
        Vfahrenheit = (Vfahrenheit)*(9/5)+(32);
            System.out.println(Vfahrenheit);
    }
    if (Vctype == "c" || Vctype == "C"){
        System.out.println("Please enter celcius");
        double Vcelcius = keyboard.nextInt();
        Vcelcius = (Vcelcius - 32)*(5/9);
               System.out.println(Vcelcius) ;
    }        
  }   
}

Hello guys I was wondering if anyone could help me with the above code. Basically in the output console in netbeans the program just seems to end after I hit C or F, but instead it should ask for a number then allow a number input, then calculate and finally display the calculation. It doesn't seem to be executing the if statements Where am I going wrong?

  • Use `Vctype.equals("f")` rather than `Vctype == "f" ` (same for the other comparisons. See the link above – David Robinson Oct 12 '13 at 16:31
  • You can't compare strings with `==`. Use `equals()` or `compareTo()`: http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html – Barranka Oct 12 '13 at 16:31
  • try "f".equalIgnorecase(Vctype) instead of (Vctype == "f" || Vctype == "F") – upog Oct 12 '13 at 16:32

1 Answers1

0

You are comparing String with ==. So it doesnt work, you have to use this :

if (Vctype.toLowerCase().equals("f"))

Note also, that using a "toLowerCase" makes the whole string lowercase, so you dont have to have two options for "F" and "f".

If you want, you can use "compareTo"

if (Vctype.toLowerCase().compareTo("f") == 0)
libik
  • 22,239
  • 9
  • 44
  • 87