We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,668 Members — Technology Publication meets Social Media

# Java calculator

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 {

}

}

}
``````
2
Contributors
7
Replies
5 Hours
Discussion Span
10 Months Ago
Last Updated
8
Views
buzmay
Newbie Poster
10 posts since Apr 2009
Reputation Points: 10
Skill Endorsements: 0

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
... trying to help
Moderator
8,683 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

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

buzmay
Newbie Poster
10 posts since Apr 2009
Reputation Points: 10
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,683 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

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?

buzmay
Newbie Poster
10 posts since Apr 2009
Reputation Points: 10
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,683 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

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;
}

}

}}
``````
buzmay
Newbie Poster
10 posts since Apr 2009
Reputation Points: 10
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,683 posts since Apr 2008
Reputation Points: 2,636