1. Member
Join Date
Jan 2010
Posts
3
Rep Power
0

Simple Help

Hello All!

I am new to Java and hope to learn this over time and progress onto EE and ME for future developments.

I am still learning Java syntax basics.

Below is some code I have written, but found when using a single line variable it gives me the wrong answer. I decided to do this with multiple variables and get the correct answer, now I have managed to problem solve the single line variable with stepping the code and found the divide is where my problems start.

Java Code:
```public class Temperature {
public int fahrenheit = 9;
public int celsius1 = fahrenheit - 32;
public int celsius2 = celsius1 * 5;
public int celsius3 = celsius2 / 9;

public int celsius = fahrenheit - 32 * 5 / 9;

// public double celsius = fahrenheit(double) - 32 * 5 / 9;

public void calculateCelsius() {

System.out.println(fahrenheit + "Fahrenheit to Celsius is " + celsius3 + "c is correct");
System.out.println(fahrenheit + " Fahrenheit to Celsius is " + celsius + "c");

}
}```
As you can see I also tried to Double this, with no success. When using a method to view the calculation it shows fine in console, just wrong numbers for one output.

I don't want to move on in learning until I understand the problem and what I did wrong.

Any help welcomed :)
Last edited by Andy_M; 01-09-2010 at 07:00 PM. Reason: Update

2. You have been bitten by the 'integer division' operator. That operator divides two ints and forgets all about the remainder. Literal numbers that don't contain a decimal point (a dot) are interpreted as ints by the compiler, so 5/9 is an integer division of the two int numbers 5 and 9; the result is 0 (that forgotten remainder, remember?).

Better use double numbers, e.g. 5.0/9.0 and use variables of type double. Try it and see for yourself.

kind regards,

Jos

3. Java Code:
`    public double celsius = (fahrenheit - 32) * 5.0 / 9.0;`
I just noticed; change your code to the above version. (multiplication has a higher precedence than subtraction).

kind regards,

Jos

4. Member
Join Date
Jan 2010
Posts
3
Rep Power
0
Jos,

This has worked a treat. So the game is to make sure I try and use double numbers IE 1.0 for doubles and make sure I close in calculation with ( ) before a higher operator?

5. Originally Posted by Andy_M
Jos,

This has worked a treat. So the game is to make sure I try and use double numbers IE 1.0 for doubles and make sure I close in calculation with ( ) before a higher operator?
Yup, it's just 101 math, e.g 2*(3+4), you add 3 and 4 first and multiply the result by 2 (14), while 2*3+4, you multiply 2 and 3 and add 4 to the result (10). It's no rocket science ;-)

kind regards,

Jos

6. Member
Join Date
Jan 2010
Posts
3
Rep Power
0
Thank you Jos for taking time out to help me

7. Originally Posted by Andy_M
Thank you Jos for taking time out to help me
You're welcome of course; always take care that you have the basics right because without them you're on quicksand and all the rest doesn't have a solid foundation.

kind regards,

Jos

Posting Permissions

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