# Help with fahrenheit to celsius conversion

• 03-05-2009, 12:42 AM
java_is_killingme
Help with fahrenheit to celsius conversion
Hi.I have recently been given the task of creating a program that converts a Fahrenheit temp to a Celsius temp with the calculateCelsius() method. The formula for the conversion is :

Celsius = (5/9) * (Fahrenheit -32)
public static float calculateCelsius(float f)

This is what I have so far, and take it easy on me I'm lost to say the least.

import javax.swing.*;

public class FahrenheitToCelsius {

public static void main (String[] args) {

// Add code to prompt user to enter a Fahrenheit temperature
// add code to invoke the method calculateCelsius and to
// display the results here.

}

public static float calculateCelsius(float f) {

return 0.0f; // so incomplete file will compile
// change the return statement to return the
// calculated Celsius value.
}
}
I'm having trouble with how to obtain the users Fahrenheit temp input and use the entered value with the supplied calculation and return an answer in another dialog box.

Thanks
• 03-05-2009, 12:45 AM
emceenugget
i have minimal experience with gui's, but seems like you need to learn them, specifically swing. here's a tutorial: Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)
• 03-05-2009, 01:14 AM
Fubarable
Or are you in fact supposed to create a GUI? If you are this new to programming, my guess is that the instructor wants you to create a program that uses a console input and output. If so, look into using a java.util.Scanner object and get your input via the Scanner.
• 03-06-2009, 10:30 AM
gwithey
something along these lines may help get you started.

if it needs to be a method just create a method which does the calculateions called "farToCelsius()" and call it by saying "result = farToCelsius(far, celsius)

Code:

```package ...package name.......; import java util.Scanner; public class .............class name.......; { public static void main(String[] args) { float celsius, fahr; int lower, upper, step; Scanner scan = new Scanner(System.in); System.out.println("Enter farenheit: "); fahr = scan.nextInt();         celsius = (5.0/9.0)*(fahr-32.0);         System.out.println("Farenheit is: " + fahr);         System.out.println("Celsius is: " + celsius);         fahr = fahr + step; }```
hope that helps a bit
• 03-07-2009, 12:11 PM
2potatocakes
Haven't tested this but you could also just use this in your main method:

String g = JOptionPane.showInputDialog("Enter in farneheit temperature: ");
float f = Float.parseFloat(g);

and then

public static float calculateCelsius(float f) {

float celsius = (5/9) * (f -32);

return celsius;
}