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 {
        System.out.println("Please select correct parameters");



    }

    }

    }

Recommended Answers

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 {
        System.out.println("Please select correct parameters");

    }
    boolean play = true;
    String answer = "Y";    
    System.out.println("Play again?");
    answer = keyboard.nextLine();
    while (play==true);
    if(answer.equalsIgnoreCase("Y")){
    play = true;
    }
    else if(answer.equalsIgnoreCase("N")){
    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 meeting, networking, learning, and sharing knowledge.