# help with second problematic kid

• 04-02-2011, 10:57 AM
elecleoalune
help with second problematic kid
hi again guys, i continued doing the personal projects after the earlier question. the tips that people gave me earlier seemed to be doing the job.
this time it's a whole new problem.
i programed a program which you input a number of different coins and it will tell you how much money you have in coins.
When you DO put in the coin values, you get a wrong value... if you enter 0's for everything, you end up with \$0.16000000001.
If you enter 1's for everything, you end up with \$4.91, when it should actually be \$1.91.
I went through the whole thing 10 times, but I just can't seem to figure it out. Help please?

/* Write an application that determines the value of the coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels, and pennies. */
import java.util.Scanner; // importing a scanner which will scan what the users will input.
public class pp0208 {
public static void main (String[] args){

double dollars, half_dollars, quarters, dimes, nickels, pennies; // units that the user will input.

Scanner scan = new Scanner (System.in); // using the scanner that had been imported in the beginning of the program.

System.out.println ("Input number of dollars: "); // input number of dollar coins
dollars = scan.nextDouble ();

System.out.println ("Input number of half-dollars: "); // input number of half-dollars
half_dollars = scan.nextDouble ();

System.out.println ("Input number of quarters: "); // input number of quarters
quarters = scan.nextDouble ();

System.out.println ("Input number of dimes:"); // input number of dimes
dimes = scan.nextDouble ();

System.out.println ("Input number of nickels:"); // input number of nickels
nickels = scan.nextDouble ();

System.out.println ("Input number of pennies:"); // input number of pennies
pennies = scan.nextDouble ();

System.out.println ("The number of coins that you input are: " + dollars + " dollars, " + half_dollars + " half-dollars, " + quarters
+ " quarters, " + dimes + " dimes, " + nickels + " nickels, and " + pennies + " pennies.");
// tells you what you input

dollars = (dollars * 1) + (half_dollars * 0.50) + (quarters * 0.25) + (dimes + 0.10) + (nickels + 0.05) + (pennies + 0.01);

System.out.println ("How much you have is: \$" + dollars); // tells you how much you have in coins

}
}
• 04-02-2011, 11:04 AM
sunde887
How come for dimes, nickels and pennies you are adding the values?

Try declaring a total variable and see why happens. The code looks good but I believe you should be multiplying smaller demoninations by the value as well.

By the way, when posting code please wrap it in code blocks to maintain indentation and make it easier for us to read
[co de]
code here
[/code]

Also make the coin variables integers, you don't want someone putting in half a quarter.
• 04-02-2011, 11:07 AM
elecleoalune
oh you are awsome!
i guess my untrained eyes are still not seeing the small errors like this.
I feel stupid for staring at this for an hour.
Thank You!
• 04-02-2011, 11:08 AM
sunde887
You fixed it?
• 04-03-2011, 10:46 AM
elecleoalune
yes! thanks to you! now i am working on the next program that might become the third problematic kid, but this time i am going to try to work on it myself. hope it goes well with it.
• 04-03-2011, 11:04 AM
sunde887
Glad to have helped. Hopefully you can figure the third one without our help, but if you cant dont hesitate to ask.