# Fahrenheit to Celsius help please

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-20-2011, 12:48 AM
Moka
Hello,

I was working on my java homework when I ran into a wall.

The assignment is to implement 2 methods (one to convert from Celsius to Fahrenheit and another for vice versa) and then to implement those in a program. I decided the best way to do this is to use a menu.

It was all working great, but now whenever I try to convert from Fahrenheit, it
always returns 0 degrees Celsius, and it seems my conversion from C to F is off as well.

I'm not sure if the problem is in the way I entered the calculations or in the way the methods are called or what.

Any help would be greatly appreciated.

PS - remember, this is a beginning Java course, so no advanced solutions please.

Here is my code:

Code:

```import java.util.Scanner; public class TempConvert {         public static void main (String [] args)         {                 Scanner keyBd = new Scanner( System.in );                 char selection;                 int convert;                 int temp;                 do{                         System.out.println("\n--------------");                         System.out.println("Main Menu\n");                         System.out.println("1. Fahrenheit to Celsius");                         System.out.println("2. Celsius to Fahrenheit");                         System.out.println("3. Exit\n");                         System.out.print  ("Selection: ");                                                 selection = keyBd.next().charAt(0);                                                 switch (selection)                         {                                 case '1':                                 {                                         System.out.print("Enter temperature in Fahrenheit: ");                                                                                 temp = keyBd.nextInt();                                                                                 convert = celsius(temp);                                                                                 System.out.printf("%d degrees F = %d degrees C",temp,convert);                                 }                                         break;                                 case '2':                                 {                                         System.out.print("Enter temperature in Celsius: ");                                                                                 temp = keyBd.nextInt();                                                                                 convert = fahrenheit(temp);                                                                                 System.out.printf("%d degrees C = %d degrees F",temp,convert);                                 }                                                break;                                 case '3':                                         break;                                 default :                                         System.out.printf("%c\n",7);                                         System.out.println("Invalid Selection");                         }                 }                 while( selection != '3');         }         public static int fahrenheit(int celsius)         {                 int fahrenheit;                 fahrenheit = (9 / 5) * (celsius + 32);                 return fahrenheit;         }                 public static int celsius(int fahrenheit)         {                 int celsius;                 celsius = (5 / 9) * (fahrenheit - 32);                         return celsius;         } }```
• 03-20-2011, 01:15 AM
Fubarable
You're doing int division, and realize that when you do this (an int divided by an int), the result is always an int. To fix this, you need to to do double division -- 5.0/9.0 and 9.0/5.0.
• 03-20-2011, 01:16 AM
ozzyman
Your Celsius to Farenheit convert is coming out wrong because you are using the inaccurate integer division (e.g. 9/5 = 1). I'm trying to figure out why the other method is returning 0's
• 03-20-2011, 01:23 AM
Moka
Ok, when I change my ints to doubles, it gives me this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from double to int

at TempConvert.celsius(TempConvert.java:68)
at TempConvert.main(TempConvert.java:30)

I'm guessing this has something to do with the methods having to return ints not doubles. I was going to change that, but my assignment specifically says "Implement the following integer methods".

Thank you guys for you quick replies!
• 03-20-2011, 01:25 AM
pbrockway2
Also note that the pair of equations you use:

fahrenheit = (9 / 5) * (celsius + 32)
celsius = (5 / 9) * (fahrenheit - 32)

are not the inverse of one another as one would expect.
• 03-20-2011, 01:26 AM
ozzyman
Okay this is the reason: in Integer division 5/9 = 0!! which equates your calculation to 0.
• 03-20-2011, 01:28 AM
Moka
Yes pbrockway2, I did notice that. These are equations I got from the book this assignment is from. I just added some parenthesis when I started having problems (I thought maybe there was an order issue or something).
• 03-20-2011, 01:30 AM
pbrockway2
Quote:

cannot convert from double to int

The compiler will not let you convert a double to an int without an explicit cast. (And returning a double value when you said you would return an int involves such a conversion.)

A cast look like this:

Code:

`int foo = (int)Math.sqrt(9);`
(int) takes the double returned by sqrt() and tells the compiler that you know what you are doing and not to worry about the inaccuracy.
• 03-20-2011, 01:30 AM
Moka
lol, thanks ozzyman. I'm still not sure how to fix this though. Should I change everything to doubles (or floats) instead of ints even though the assignment says "integer methods"?
• 03-20-2011, 01:31 AM
ozzyman
if the assignment says integer methods just use integer methods but multiply by 100 before you make any calculation on it? - or there must be a way around it
• 03-20-2011, 01:33 AM
pbrockway2
Indeed it is an order issue. Wikipedia is your friend.
• 03-20-2011, 01:34 AM
ozzyman
i just found this on the net:

In order to convert temperature to Celsius from Fahrenheit, subtract the Fahrenheit number by 32, multiply by 100 and divide by 180. Its better than 9/5!
• 03-20-2011, 01:36 AM
Moka
This book doesn't know that. This is the exact way it tells me to enter the calculations:

celsius = 5.0 / 9.0 * ( fahrenheit - 32 );

fahrenheit = 9.0 / 5.0 * celsius + 32;

and that is how I had them in my very original code (before some tweeking).
• 03-20-2011, 01:37 AM
pbrockway2
No don't multiply by 100!

Return what you calculate, but cast it to an int. Or, better perhaps, round it to an int. The assignment really should have said which to do if it said you must return an int. If it didn't I would go for casting because it's easier (and I'm lazy). I would add a note to the method recognising the inherent inaccuracy of the returned value.
• 03-20-2011, 01:41 AM
Moka
I have no experience with "casting", and I'm not sure what you mean by that. The example you showed earlier makes sense, but I'm unclear as to how to implement it into my code.
Thank you guys again for helping!
• 03-20-2011, 01:41 AM
ozzyman
actually wait, this is the problem, re-arrange the brackets:
for 72 fahrenheigt, 72-32 = 40
40* (5/9) in integer division = 40* 0
but
(40*5)/9 in integer division = 200 / 9 = 22
a double will give you the answer 22.2222222 so its not much difference
• 03-20-2011, 01:41 AM
pbrockway2
The book has it right, I think. (But I would still check with Wikipedia) Notice how for celsius the subtraction is done first and for fahrenheit the corresponding addition is done last. The reversal is how it should be.

In your equations you do both the addition and the subtraction before the multiplication.
• 03-20-2011, 01:45 AM
pbrockway2
Try what I posted. I don't know what you code is at the moment. But whereever you assign a double expression to an int, or return a double expression where you said you would return an int, put (int) in front of the expression. Often it looks like this:

Code:

```int foo = (int)(/*some double expression*/); // or return (int)(/*some double expression*/);```
• 03-20-2011, 01:48 AM
Moka
Something like this?:

Code:

```public static int fahrenheit(int celsius)         {                 int fahrenheit;                 fahrenheit = (int)9.0 / (int)5.0 * celsius + 32;                 return fahrenheit;         }                 public static int celsius(int fahrenheit)         {                 int celsius;                 celsius = (int)5.0 / (int)9.0 * (fahrenheit - 32);                         return celsius;         }```
This removes the errors, but it still returns 0 C every time...
• 03-20-2011, 01:53 AM
pbrockway2
No - just a single cast:

Code:

```    /**     * Returns the temperature in a whole number of degrees F.  The result is rounded     * towards zero.     */ public static int fahrenheit(int celsius) {     int fahrenheit;     fahrenheit = (int)(9.0 / 5.0 * celsius + 32);     return fahrenheit; }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last