# Strange Error

• 09-17-2013, 10:45 PM
Zelaine
Strange Error
Hello!

My program is supposed to convert degrees Fahrenheit to degrees Celsius, but somehow it always outputs 0, and I don't know why. Maybe you do?

Code:

```import javax.swing.*; public class attakomma1{         public static void main(String[] args){                 String s = JOptionPane.showInputDialog("Enter a temperature in degrees Fahrenheit");                 double temperatur = Double.parseDouble(s);                 temperatur = (temperatur-32)*(5/9);                 JOptionPane.showMessageDialog(null, "Here is the temperature in degrees Celsius: " + temperatur);         } }```
• 09-17-2013, 11:01 PM
Kuttappu
Re: Strange Error
problem is in 5/9
it will give answer as 0, since it is integer division..
you can solve it by replacing that statement by

temperatur = (temperatur-32)*((double)5/(double)9);
• 09-17-2013, 11:15 PM
Zelaine
Re: Strange Error
Oh, thanks :) Isn't just writing (double)5/9 sufficient?
• 09-17-2013, 11:54 PM
jim829
Re: Strange Error
Why not just write 5/9.0

Regards,
Jim
• 09-18-2013, 02:36 AM
Re: Strange Error
Quote:

Isn't just writing (double)5/9 sufficient?
No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.

Quote:

5/9.0
that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.
• 09-18-2013, 02:57 AM
jim829
Re: Strange Error
Uh, actually (double)5/9 or 5.0/9 also works.

Regards,
Jim
• 09-18-2013, 03:05 AM
Junky
Re: Strange Error
5.0 / 9

The 9 will be promoted to a double before the division is performed. Thus giving a double result. Java will never lose precision unless you tell it via a cast or some other truncating method.
• 09-18-2013, 01:05 PM
gimbal2
Re: Strange Error
Quote:

Originally Posted by Kuttappu
It wont work. Answer for it will be 0.0

Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.
• 09-18-2013, 01:07 PM
Kuttappu
Re: Strange Error
Quote:

Originally Posted by quad64bit
No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.

that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.

both will work actually. you just need one of the operands to be in double, doesnt matter if it is 5.0/9 or 5/9.0
• 09-18-2013, 01:09 PM
Kuttappu
Re: Strange Error
Quote:

Originally Posted by gimbal2
Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.

yes. u are right, it works.. I tried like (double)(5/9).. My bad..
• 09-18-2013, 05:51 PM