Hi,

I am trying to do a calculator for celcius to farenheit and viceversa. However I cannot understand why my program does not work. After reading the scanner it does anything and I really do not know why. Could anybody give me a hand here is what i have gotten (please be nice I am new to Java :D).

``````import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Choose F for farneheit to celsius, C for Celsius to farenheit:");

String option = keyboard.next();

if (option == "F" || option == "f")
{

System.out.println("Enter a farenheit Degree:");
int number = keyboard.nextInt();

double farenheit = number;
double celsius = (5.0/9) * (farenheit - 32);
System.out.println(number + "In Farenheit degrees is" + celsius + "in celsius degree");

}
else if (option == "C" || option == "c")

{
System.out.println("Enter a celcius Degree:");
int number = keyboard.nextInt();

double celcius = number;
double farenheit = (celcius * 1.8) + 32;
System.out.println(number + "In Celsius degrees is" + farenheit + "in farenheit degree");

}
else {

}

}

}
``````

if (option == "F" || option == "f")

Here's your problem. == tests for two objects being exactly the same object, but option is something you just read from the scanner, and "F" is a String created at compile time - they are two different objects, so the …

No, = is assignment, so option = "C" assigns the value "C" to option, then the || makes no sense so it errors.
== returns false because they are two different objects, it's as simple as that. The only version that works is
if (option.equals("C") || option.equals("c"))
or, of course, …

## All 7 Replies

if (option == "F" || option == "f")

Here's your problem. == tests for two objects being exactly the same object, but option is something you just read from the scanner, and "F" is a String created at compile time - they are two different objects, so the == test is always false.
To compare Strings you can use the equals method which tests that the strings have the same sequence of letters in them. Even better, for this case, is equalsIgnoreCase method, which does what it says. So...
`if (option.equalsIgnoreCase("f")) ...`

JamesCherrill: Thanks a lot for the help I used if (option.equalsIgnoreCase("f")) and works perfectly. However I am curious about what you said about the == returning false. Do you mean that I shall use

``````if (option = "C" || option = "c")
``````

because the compiler complains.

Million thanks

Here's your problem. == tests for two objects being exactly the same object, but option is something you just read from the scanner, and "F" is a String created at compile time - they are two different objects, so the == test is always false.
To compare Strings you can use the equals method which tests that the strings have the same sequence of letters in them

No, = is assignment, so option = "C" assigns the value "C" to option, then the || makes no sense so it errors.
== returns false because they are two different objects, it's as simple as that. The only version that works is
if (option.equals("C") || option.equals("c"))
or, of course, the ignoresCase version that does the same thing.

thanks for the help, do you happen to know how I can do that program to never stop, I mean if you see my if else conditions, the else if will make the program quits, if the user gives something else than F or C. But how can i do so if the user press for instance D, the program prints and prompts the user to either choose C or f?

You do that with a `while` loop or a `do/while` loop - lots of examples and tutorial stuff on the web, like this one

sorry being trying to find a solution but now im confuse and tired. Will try tomorrow, but if you would like to guide I would appreciate it a lot here is what I have come with

``````import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
System.out.println("Choose F for farneheit to celsius, C for Celsius to farenheit:");

String option = keyboard.next();

while (true){

if (option.equalsIgnoreCase("f")) {

System.out.println("Enter a farenheit Degree:");
int number = keyboard.nextInt();

double farenheit = number;
double celsius = (5.0/9) * (farenheit - 32);
System.out.println(number + " In Farenheit degrees is " + celsius + " in celsius degree");

}
else if (option.equalsIgnoreCase("c"))

{
System.out.println("Enter a celcius Degree:");
int number2 = keyboard.nextInt();

double celcius = number2;
double farenheit = (celcius * 1.8) + 32;
System.out.println(number2 + " In Celsius degrees is " + farenheit + " in farenheit degree");

}

else {

}
boolean play = true;
System.out.println("Play again?");
while (play==true);
play = true;
}
play=false;
}

}

}}
``````

You have all the right stuff - it's just not in the right place!
You have two while loops, but one is all you need.
The loop in lines 47-57 is basically right, but that needs to encompass the whole thing, not just be there on its own at the end of the program.
The while at line 15 is in the right place, but it should have the "play" boolean logic.
So take the code for the 47-57 loop and use that to replace the while (true) on line 15.
You're 95% there, and it will probably look a lot simpler after a good night's sleep.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.
{"ft_lang_label":"__label__en","ft_lang_prob":0.676917,"math_prob":0.8804621,"size":1195,"snap":"2021-31-2021-39","text_gpt3_token_len":310,"char_repetition_ratio":0.1536524,"word_repetition_ratio":0.021505376,"special_character_ratio":0.29456067,"punctuation_ratio":0.19911504,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9703186,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-23T16:02:03Z\",\"WARC-Record-ID\":\"<urn:uuid:28551bd1-8f99-4b46-a398-4e7862970726>\",\"Content-Length\":\"87494\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ac7052ac-1585-499d-a831-9a90f0040cde>\",\"WARC-Concurrent-To\":\"<urn:uuid:e9dc4b73-a961-49f4-ae54-f62e25bea3e7>\",\"WARC-IP-Address\":\"104.22.4.5\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/430808/java-calculator\",\"WARC-Payload-Digest\":\"sha1:S6ZPBUO3Q3DXWBBA6LU5VO2CTKRFWWRA\",\"WARC-Block-Digest\":\"sha1:5HHLH3MCEVEAL4KCPRDEPWWBRTVCHIIN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046149929.88_warc_CC-MAIN-20210723143921-20210723173921-00144.warc.gz\"}"}