Results 1 to 7 of 7
Thread: Simple Help
 01092010, 06:58 PM #1Member
 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"); } }
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; 01092010 at 07:00 PM. Reason: Update
 01092010, 07:04 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,050
 Blog Entries
 7
 Rep Power
 23
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
 01092010, 07:08 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,050
 Blog Entries
 7
 Rep Power
 23
Java Code:public double celsius = (fahrenheit  32) * 5.0 / 9.0;
kind regards,
Jos
 01092010, 07:12 PM #4Member
 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?
 01092010, 08:45 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,050
 Blog Entries
 7
 Rep Power
 23
 01092010, 09:06 PM #6Member
 Join Date
 Jan 2010
 Posts
 3
 Rep Power
 0
Thank you Jos for taking time out to help me
 01092010, 10:10 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,050
 Blog Entries
 7
 Rep Power
 23
Similar Threads

Simple AI?
By Atriamax in forum New To JavaReplies: 4Last Post: 12232009, 11:35 PM 
New simple application using a simple database
By webbusiness23 in forum New To JavaReplies: 9Last Post: 08032009, 02:55 AM 
Simple Help....
By jpnym15 in forum New To JavaReplies: 1Last Post: 11112008, 03:33 PM 
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05042008, 05:27 PM 
simple GUI
By dim_ath in forum New To JavaReplies: 3Last Post: 01072008, 04:00 PM
Bookmarks