1. Member
Join Date
Mar 2011
Posts
14
Rep Power
0

## Fahrenheit to Celsius help please

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:

Java 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("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;
}

}```

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

3. 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
Last edited by ozzyman; 03-20-2011 at 01:21 AM.

4. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
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!

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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.

6. Okay this is the reason: in Integer division 5/9 = 0!! which equates your calculation to 0.

7. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
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).

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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:

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

9. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
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"?

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

11. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Indeed it is an order issue. Wikipedia is your friend.

12. 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!

13. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
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).

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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.

15. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
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!

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

17. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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.

18. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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:

Java Code:
```int foo = (int)(/*some double expression*/);

// or

return (int)(/*some double expression*/);```

19. Member
Join Date
Mar 2011
Posts
14
Rep Power
0
Something like this?:

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

20. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
No - just a single cast:

Java 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;
}```

Page 1 of 2 12 Last

#### Posting Permissions

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