| Author |
Why am I not getting the right answer
|
Ian Lubelsky
Ranch Hand
Joined: Feb 10, 2010
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
Joined: Feb 12, 2003
Posts: 78
|
|
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
Joined: Feb 10, 2010
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
Joined: Feb 10, 2009
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.
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Ian Lubelsky
Ranch Hand
Joined: Feb 10, 2010
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
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Serap Elbeyoglu
Ranch Hand
Joined: Feb 12, 2010
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.
|
Serap Elbeyoglu
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Serap Elbeyoglu wrote:Choosing int will solve your problem.
No it won't. That has already been demonstrated.
|
 |
Ian Lubelsky
Ranch Hand
Joined: Feb 10, 2010
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Why am I not getting the right answer
|
|
|