Results 1 to 20 of 24
 03192011, 11:48 PM #1Member
 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("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; } }

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.

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; 03202011 at 12:21 AM.
 03202011, 12:23 AM #4Member
 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!
 03202011, 12:25 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
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.

Okay this is the reason: in Integer division 5/9 = 0!! which equates your calculation to 0.
 03202011, 12:28 AM #7Member
 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).
 03202011, 12:30 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
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);
 03202011, 12:30 AM #9Member
 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"?
 03202011, 12:31 AM #10
 Join Date
 Mar 2011
 Location
 London, UK
 Posts
 797
 Blog Entries
 2
 Rep Power
 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
 03202011, 12:33 AM #11Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Indeed it is an order issue. Wikipedia is your friend.
 03202011, 12:34 AM #12
 Join Date
 Mar 2011
 Location
 London, UK
 Posts
 797
 Blog Entries
 2
 Rep Power
 10
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!
 03202011, 12:36 AM #13Member
 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).
 03202011, 12:37 AM #14Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
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.
 03202011, 12:41 AM #15Member
 Join Date
 Mar 2011
 Posts
 14
 Rep Power
 0
pb, please clarify.
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!
 03202011, 12:41 AM #16
 Join Date
 Mar 2011
 Location
 London, UK
 Posts
 797
 Blog Entries
 2
 Rep Power
 10
actually wait, this is the problem, rearrange the brackets:
for 72 fahrenheigt, 7232 = 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
 03202011, 12:41 AM #17Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
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.
 03202011, 12:45 AM #18Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
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*/);
 03202011, 12:48 AM #19Member
 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; }
 03202011, 12:53 AM #20Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Similar Threads

Celsius to Fahrenheit
By Danieldcc in forum New To JavaReplies: 2Last Post: 09302010, 04:50 AM 
Fahrenheit to Celcius program error
By bosoxfan in forum New To JavaReplies: 3Last Post: 02102010, 03:38 AM 
Help with fahrenheit to celsius conversion
By java_is_killingme in forum New To JavaReplies: 4Last Post: 03072009, 11:11 AM 
Fahrenheit to celsius
By lalithalydia in forum New To JavaReplies: 3Last Post: 02052008, 07:50 AM 
Converts from Fahrenheit to Celsius
By trill in forum New To JavaReplies: 1Last Post: 08062007, 05:52 AM
Bookmarks