I got this to run but it will not display the final result. It ask for C or F then the temp degree but will not display the conversion. The process just completes. Any ideas? I am new here....
Thanks

My project::-/

import javax.swing.JOptionPane;
public class Assign8_Cheshire
{
    public static void main(String[] args)
    {
	double result;
	String degree;
	int input;

	degree = JOptionPane.showInputDialog("Enter: \n C - Fahrenheit to Celsius\n F - Celsius to Fahrenheit");
	input = new Integer(JOptionPane.showInputDialog("Enter the degree: ")).intValue();

	if(degree == "C" || degree == "c")
	{
			result = 5.0/9.0 * (input - 32);
			JOptionPane.showMessageDialog(null," Fahrenheit is " + result + " Celsius");
	}
	else if(degree == "F" || degree == "f")
	{
		result = 9.0/5.0 * input + 32;
		JOptionPane.showMessageDialog(null, " Celsius is " + result + " Fahrenheit");
	}

    }
}

Recommended Answers

All 6 Replies

When testing Strings for equality, use the equals() method. The == operator does not test the contents of an object. It tests if two pointers point to the same object.

Also look at the method that ignores case.

I appreciate your reply ...I am really confused now. Which == should I change?

The ones used with Strings.
You should only use == with things that are NOT class objects.

I am so Java stupid. I can't get it to work right. I thought I was on the right track based on my text book. Our professor just gives projects with no hints. Could you give me an example of how to code it correctly?

Show a sample of what you've tried.
Do you know how to use a class's method?
Have you read the API doc for the String class?

So to clarify... (hopefully)

The '==' operator is only used with primitive variable types--int, double, boolean, etc. Strings are a type of Object; they did not "come with the system" but rather first had to be programmed in Java to define their behavior. For most objects obj, the .equals(obj) method is used to compare them.

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.