Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Why am I not getting the right answer

Ian Lubelsky
Ranch Hand
Posts: 49
Hello to one and all.

I am starting the journey of learning java. I am following a book I have, that gives lab assignments at the end. The assignment I am having a problem with at the moment is to convert a Fahrenheit temperature to Celsius. When I run the following code, I get an answer of "0" for Celsius when the information is printed to screen. Can someone explain to me what is wrong.

Thanks in advance for any help.

Dave Trower
Ranch Hand
Posts: 86
The problem with your code is the 5/9 is resolving to 0.
This is because 5 and 9 are integers by default.
One way to fix the problem is to do what is known as a primitive cast to tell the computer 5 and 9 are doubles.
Change you line to this and will work as you expect

Ian Lubelsky
Ranch Hand
Posts: 49
Holy Java Batman, it worked.

Thanks for the info.

However, I thought by including "double" at the beginning of the line: "double Celsius = ......" would make the whole line a double.

Does this mean when I use "double" I have to add a "D" at the end like when I do a "Long" by adding an "L" at the end?
I tried looking up the answer in the book I'm using, but some of the examples I see when using double, have a "D" at the end and others don't. Am I better off just adding a "D" at the end regardless?

W. Joe Smith
Ranch Hand
Posts: 710
Ian Lubelsky wrote:Holy Java Batman, it worked.

Thanks for the info.

However, I thought by including "double" at the beginning of the line: "double Celsius = ......" would make the whole line a double.

Does this mean when I use "double" I have to add a "D" at the end like when I do a "Long" by adding an "L" at the end?
I tried looking up the answer in the book I'm using, but some of the examples I see when using double, have a "D" at the end and others don't. Am I better off just adding a "D" at the end regardless?

No, saying "double Celsius = ..." only denotes that the variable Celsius is a double.

Ian Lubelsky
Ranch Hand
Posts: 49
W. Joe Smith wrote:
Ian Lubelsky wrote:Holy Java Batman, it worked.

Thanks for the info.

However, I thought by including "double" at the beginning of the line: "double Celsius = ......" would make the whole line a double.

Does this mean when I use "double" I have to add a "D" at the end like when I do a "Long" by adding an "L" at the end?
I tried looking up the answer in the book I'm using, but some of the examples I see when using double, have a "D" at the end and others don't. Am I better off just adding a "D" at the end regardless?

No, saying "double Celsius = ..." only denotes that the variable Celsius is a double.

After re-reading the section about double, float, long, I now realize I was a double only to Celsius and not directly to the values I was calculating.

Henry Wong
author
Marshal
Posts: 21117
78
Ian Lubelsky wrote:
I tried looking up the answer in the book I'm using, but some of the examples I see when using double, have a "D" at the end and others don't. Am I better off just adding a "D" at the end regardless?

You actually don't need "D" in many cases. Specifying a decimal will work too.... meaning 5.0 / 9.0 works. Also, if it is mixed, then it will uses the type with the most range.... meaning 5.0 / 9 will also work. Since you are dividing a double value of 5.0 with an integer of 9, it will know that the result is to be a double.

Henry

Serap Elbeyoglu
Ranch Hand
Posts: 52
Ian you asked a good question. Answers are very clear to understand what is the problem in here. It is just about data type. Choosing int will solve your problem.

Campbell Ritchie
Sheriff
Posts: 48953
60
Serap Elbeyoglu wrote:Choosing int will solve your problem.
No it won't. That has already been demonstrated.

Ian Lubelsky
Ranch Hand
Posts: 49
Henry Wong wrote:
Ian Lubelsky wrote:
I tried looking up the answer in the book I'm using, but some of the examples I see when using double, have a "D" at the end and others don't. Am I better off just adding a "D" at the end regardless?

You actually don't need "D" in many cases. Specifying a decimal will work too.... meaning 5.0 / 9.0 works. Also, if it is mixed, then it will uses the type with the most range.... meaning 5.0 / 9 will also work. Since you are dividing a double value of 5.0 with an integer of 9, it will know that the result is to be a double.

Henry

I understand now I can use a "D" at the end or a decimal, or I could just write ((double)5/9).
However for future reference, what is more widely accepted way of coding or is just up to the coder to decide?

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Ian Lubelsky wrote:However for future reference, what is more widely accepted way of coding or is just up to the coder to decide?

It's a question of style, I would have written 5.0 / 9.0 myself. I don't think there is one generally agreed upon way to write this.

Rob Spoor
Sheriff
Posts: 20527
54
I usually use .0 as well. I think you shouldn't cast literals; not only are 5D and 5.0 shorter than (double)5, it also shows you have better knowledge of the language. I only use a cast if both operands are non-double variables: