I need help on my project. This is what I have so far:

import javax.swing.JOptionPane;

public class Convert{
	public static void main(String[] args){

		String choice = "";

		do {
		choice = JOptionPane.showInputDialog(null, "Would you like to enter a temperature?");
		}

		while(choice.equals("No") | choice.equals("no") | choice.equals("N") | choice.equals("n"));{
		choice = JOptionPane.showInputDialog(null, "What is the temperature you would like to input?");
		choice = JOptionPane.showInputDialog(null, "Is the temperature C (for celsius) or F (for Farenheit)?");
		}





	}
}

I have to ask the user if they want to enter a temperature, and with that response I have to ask them what the temp is, whether its Celsius or Farenheit, and give them the conversion.
Any suggestions?
Appreciate it!

Recommended Answers

Okay, so the formulas for temperature are:
F = 1.8*C + 32
C = (F-32)/1.8

try incorporating these into your program using if/else statements checking for whether or not it is celcius.

After you do this, come back with your new code and i can help you out …

Jump to Post

All 4 Replies

Okay, so the formulas for temperature are:
F = 1.8*C + 32
C = (F-32)/1.8

try incorporating these into your program using if/else statements checking for whether or not it is celcius.

After you do this, come back with your new code and i can help you out more if you still need it.

TJ

This is what I have now. Any more suggestions?

import javax.swing.JOptionPane;
	public class Convert{
		public static void main(String[] args){
		String choice = "";
		int result;
		int degree;

		while(choice.equals("Yes") || choice.equals("yes") || choice.equals("YES") || choice.equals("y") || choice.equals("Y"));
		choice = JOptionPane.showInputDialog(null,"Would you like to enter a temperature?");

	result = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the temperature you would like to have converted?"));
	degree = JOptionPane.showInputDialog(null, "Is the temperature c (for celsius) or f (for Farenheit)?");

	public static double celsiusToFarenheit(double celsius){
		if (degree == c)
		celsius = (5.0/9) * Farenheit - 32;

	}

	public static double farenheitToCelsius(double farenheit){
		if (degree == f)
		farenheitt = (9.0/5) * celsius + 32;

	}
	}
}

Your program isn't going to work. In your method celciusToFarenheit, you tried to compare with "if degree == c". However, if you look at this link you will see that the JOptionPane class's showInputDialog method (the one you called, where you said degree = JOptionPane.showInputDialog...) returns a String which is an Object. Since you cannot use "==" to compare Objects for equality you need to use a method such as the equals() method to test if two Strings are equal.

Another reason why you cannot use degree = JOptionPane.showInputDialog is because no showInputDialog method exists that returns an int. Did you even run your code? I could be mistaken and be looking at the wrong doc or something... but I'm pretty sure if you run your code it is going to give you a compiler error for what I just mentioned, and even if you got past that, you have the logical error with trying to compare things using '==' that only works for ints (and char, etc)

I knew that one had a lot of errors. It was what I just had come up with after working on it for a while. This is what I am working with now:

import javax.swing.JOptionPane;
	public class Conversion{
		public static void main(String [] args){

		String choice = "";
		double fahrenheit = 0;
		double celsius = 0;
		double c = 0;
		double f = 0;


		while(choice.equals("Yes") || choice.equals("yes") || choice.equals("Y") || choice.equals("y"));{
		choice = JOptionPane.showInputDialog(null, "Would you like to enter a temperature for conversion?");
		}
		Integer.parseInt(JOptionPane.showInputDialog(null, "What is the temperature you would like to have converted?"));
		JOptionPane.showInputDialog(null, "Is the temperature c (for Celsius) or f (for Fahrenheit)?");
	}

	public static double celsiusToFahrenheit(double celsius){
		double celsius = (5.0/9 * fahrenheit - 32);
		JOptionPane.showMessageDialog(null, "The temperature is " + celsius);
	}
	public static double fahrenheitToCelsius(double fahrenheit){
		double fahrenheit = (9.0/5 * celsius + 32);
		JOptionPane.showMessageDialog(null, "The temperature you entered is " + fahrenheit);
	}
}

these are the 4 errors I end up with:

C:\Documents and Settings\student\My Documents\Conversion.java:20: celsius is already defined in celsiusToFahrenheit(double)
		double celsius = (5.0/9) * fahrenheit - 32;
		       ^
C:\Documents and Settings\student\My Documents\Conversion.java:20: cannot find symbol
symbol  : variable fahrenheit
location: class Conversion
		double celsius = (5.0/9) * fahrenheit - 32;
		                           ^
C:\Documents and Settings\student\My Documents\Conversion.java:24: fahrenheit is already defined in fahrenheitToCelsius(double)
		double fahrenheit = (9.0/5) * celsius + 32;
		       ^
C:\Documents and Settings\student\My Documents\Conversion.java:24: cannot find symbol
symbol  : variable celsius
location: class Conversion
		double fahrenheit = (9.0/5) * celsius + 32;
		                              ^
4 errors

Tool completed with exit code 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.