0
import java.util.Scanner;
import java.lang.Math;
public class ConvertTemp 
{

    public static void main(String[] args) 
    //try making a program that asks if the user wants to convert f to c or vv
    {
        //declare variables and instantiate
        double temp, fahren, cels;
        String tempType;
        Scanner input = new Scanner(System.in); //try changing this later 


        //ask user for data
        System.out.print("Is this temperature in fahrenheit or celsius (enter F or C)?");
        tempType = input.nextLine();

        System.out.println(tempType);
        System.out.println(tempType=="F");

        if(tempType == " F")
        {
            System.out.print("Enter your temperature in Fahrenheit: ");
            temp = input.nextDouble();
            fahren = Math.round((9.0/5.0) * (temp + 32.0));
            System.out.println(temp + " degrees fahrenheit is " + fahren);
        }

        if(tempType == " C") 
        {
            System.out.print("Enter your temperature in Celsius: ");
            temp = input.nextDouble();
            cels = Math.round((temp - 32.0) * 5.0/9.0); //this will round to one decimal bc float param
            System.out.println(temp + " degrees celsius is " + cels);
        }


    }

}

When I run the program and enter F, this is what shows up:

Is this temperature in fahrenheit or celsius (enter F or C)?F F false

Why is it giving me false? Even before I try running through the if statements?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
Nat
  • 1
  • You have probably realized by now you should have used equals(), but in your case I recommend equalsIgnoreCase(). – Killer Death Sep 03 '17 at 21:27
  • Why does == not work for strings? I tried searching and someone mentioned that == only works for integers/doubles? – Nat Sep 03 '17 at 21:32
  • You have a link to another topic placed by moderators, this one cannot receive answers because it is marked as duplicate and I cannot write it as a comment, it's a long story – Killer Death Sep 03 '17 at 21:34
  • Thank you so much! Sorry this is my first time on stackoverflow and I didn't exactly know how the site worked – Nat Sep 03 '17 at 21:37
  • No problem, cheers – Killer Death Sep 03 '17 at 21:39

0 Answers0