# Thread: Help with fahrenheit to celsius conversion

1. Member
Join Date
Mar 2009
Posts
1
Rep Power
0

## 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

2. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
12
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)

3. 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.

4. Member
Join Date
Mar 2009
Posts
18
Rep Power
0
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)

Java 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

5. Member
Join Date
Sep 2008
Posts
43
Rep Power
0
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;
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•