So I feel like SUCH a n00b but I'm fed up of mulling over all this myself when I'm sure there are dudes on this site that can just tell me what I'm doing wrong. I've tried Google so, don't send me that way, I wouldn't be asking if I wasn't stuck.
So as part of the first year of my degree I have to do Java, and, what initially I thought would be easy to get to grips with is actually not turning out so well for me. I'm fine at manipulating already written code, but writing from scratch is like, a nightmare.
Anyway - so I have to make a body mass index calculator as one of the basic exercises (I'm on like Question 6 of 58 *sigh*, hence the thread) -- anyway, I'm having issues with division, so I've created up a little test app to try and work out why division doesn't seem to be working.
Here's my code -
public class test {
public static void main(String[] args) {
double divide;
divide = 183 / 100;
System.out.println(divide);
}
}
Now we all know right, 183 / 100 is 1.83 -- so why is Java giving me the result "1.0" -- exactly what is wrong with that code?
Really appreciate the (n00b) help in advance.
Can Anyone Help Me Finish My Java Malarky?
Mainly because I'm not entirely sure what I'm doing. Either way, does that declaration give the correct answer....
*tries*
Sadly, no. It still tells me the answer is "1.0"; any ideas?
LOL. Haha - that's why I felt awkward about asking, because I knew the solution was going to be something really simple.
That does indeed work. Thank you very much! Seriously, thank you -- I spent 2 hours on that! :(
"Word.I have no doubt I'll be back ;)"
Post other ones up if ya need help. These are fun.
Or you could properly type cast it. There other methods of doing so.
One way would times it by divide = (1.00*183 )/ (1.00*100);
Another way is divide = (double)183/ (double)100;
The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before.
"Or you could properly type cast it. There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows.
I'm taking a Java Programming this semester, too. I took it in high school, but I need this class for my major (Web Design).
I should do some more programming, maybe get some practice on parsing (I made a semi-functional IRC client in Java but the parsing code was spaghetti)
"Man, I remember doing stuff like this in my first two years of high school. Yep... forgot almost everything. lol."I forgot most of the Java stuff I learned in high school, but it came back to me pretty quickly in my class this semester.
"Godwind said:My opinion, always think of the broader case. Sammy shouldn't be able to just fix a small problem that he came across in problem, he should know how to solve all the problems that come his way. He should understand the concept that floating decimal point value has a decimal being stored, which is not registered values of an integer. Since calculations occur right to left, integers will behave as integers, not as doubles to accommodate his problems."Or you could properly type cast it. There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows."
"Jayge said:No arguments here."Godwind said:My opinion, always think of the broader case. Sammy shouldn't be able to just fix a small problem that he came across in problem, he should know how to solve all the problems that come his way. He should understand the concept that floating decimal point value has a decimal being stored, which is not registered values of an integer. Since calculations occur right to left, integers will behave as integers, not as doubles to accommodate his problems. ""Or you could properly type cast it. There other methods of doing so.One way would times it by divide = (1.00*183 )/ (1.00*100);Another way is divide = (double)183/ (double)100;The reason why I say this if your user input is NOT a float decimal value but an integer, you will encounter troubles using the method presented from before."That's true. I was just showing him how to fix his smaller issue though. It seems he hasn't ran into trouble yet, so who knows."
Please Log In to post.
Log in to comment