some help with numbers and java

lockmac

Senior member
Dec 5, 2004
603
0
0
hi guys.

im trying to make my first java program and coming across problems.
my program simulates a coffee machine, where the final amount is displayed and the user selects what coin they insert... the next screen then shows the user how much money they have left... but everytime i have my variable (its a double) take away the money they have inserted (eg.... totalPrice (1.80) - 1), it comes up with an answer like .7999999994 instead of .8.

Anybody able to help?
I also tryed storing them in integers and having it in cents (eg. 180cents), but am not sure how to put the decimal point in when display to the user

Any help appreciated
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
That's the thing with doubles (it's actually an IIEE spec, not a java thing). Don't worry about it too much, just make sure you use a java.text.NumberFormat class to round the values before you display them.
 

lockmac

Senior member
Dec 5, 2004
603
0
0
Hey thanks for the reply... where would i find this java.text.NumberFormat class?

Many thanks
 
Sep 29, 2004
18,656
68
91
Originally posted by: lockmac
Hey thanks for the reply... where would i find this java.text.NumberFormat class?

Many thanks

Enjoy - you might want to bookmark this link by the way along with Sun's java tutorials.
 

lockmac

Senior member
Dec 5, 2004
603
0
0
Thanks for the link mate!

Also im having some more small troubles... i have a do while loop.. is it possible to put two conditions that are OR'ed together... what i mean is the loop will finish if a value of the variable equals 1069(as in my case) or it will end if the value equals 3(as in my case)... i can get each one working fine with just the one number, but when i put an or statement in their, none of them work..

i know this might be a bit hard as you dont have my code but any help would be greatly appreciated as this small problem is really spoiling my saturday morning!

many thanks
 

BigPete

Senior member
May 28, 2001
729
0
0
Yes, but you have to remember that conditional expressions are evaluated from left to right. So if you have an OR statement and the first condition is true, the second condition will never be evaluated. Since the value "3" is what ends the loop, thats what needs to be evaluated first, since if that's true, nothing needs to be done. If the value does equal 3, then you want to evaluate if some other value is 1069 (which I assume would be if the other value is not 3). If the other value is 1069, the loop will run. It will continue like this until your "3" value is 3.
 

lockmac

Senior member
Dec 5, 2004
603
0
0
hi guys sorry just quickly... at the very end of my program.. i want it to say "Press any key to go back to the start". At the moment, once the program completes, it does go back to the start, but I would like to pause it at the very end where this comes up and asks the user to press any key to continue the program (which is already programmed to go back to the start).

Any ideas? many thanks
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Originally posted by: lockmac
hi guys sorry just quickly... at the very end of my program.. i want it to say "Press any key to go back to the start". At the moment, once the program completes, it does go back to the start, but I would like to pause it at the very end where this comes up and asks the user to press any key to continue the program (which is already programmed to go back to the start).

Any ideas? many thanks
You can use an InputStreamReader to get the character the user enters. Since you are asking for any key, it doesn't matter what key they press, but it will pause the program until a character is entered. Some example code is below:
 

lockmac

Senior member
Dec 5, 2004
603
0
0
hi thanks for the reply.. i tryed this and i am just getting errors saying "canot find symbol class InputStreamReader...

i have the "InputStreamReader cin = new InputStreamReader(System.in);" at the top of my code where I also have a NumberFormat in their as well.

Im sure its a simple thing but im not too advanced yet with java

Many thanks
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
It means java doesn't know where to find InputStreamReader. You can tell it by importing InputStreamReader's package. In this case, "import java.io.*;" at the top of your code.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Originally posted by: lockmac
hi thanks for the reply.. i tryed this and i am just getting errors saying "canot find symbol class InputStreamReader...

i have the "InputStreamReader cin = new InputStreamReader(System.in);" at the top of my code where I also have a NumberFormat in their as well.

Im sure its a simple thing but im not too advanced yet with java

Many thanks
Oops. Jeez, I'm sorry. Yeah, as flashbacck said you need to import the library with the "import java.io.*;" at the top of your code.