This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Change Due/Tendered Program

Benjamin Chau
Ranch Hand
Posts: 49
My other post for this program took on a different path so I'd like to address this program again, since I'm still trying to figure it out. Here it is...

INSTRUCTIONS:
You are to create a simple change program. The user enters the amount due and the amount tendered. You are to calculate how much change they should receive. You are then to break down the change in \$1 bills, quarters, dimes, nickels, and pennies such that the user will receive the least amount of coins. For example, \$0.80 is 3 quarters and a nickel not 8 dimes or 80 pennies.

Remember to make the program user-friendly. Check for non-numeric characters and insufficient amounts tendered. Have the program loop so that I can test different cases.

I haven't tried to compile yet because of a few issues. The program is to determine if the user's input is valid. Is there certain syntax I can accomplish this? I know that my syntax above is wrong but that's what I want to accomplish. Here's the section of that code:

Here's what I'm thinking that would work better for that section of syntax. I didn't see any harm in assigning a variable to itself.

Any thoughts? Any help would be greatly appreciated. Thanks!

Benjamin

[ November 21, 2008: Message edited by: Benjamin Chau ]

[ November 21, 2008: Message edited by: Benjamin Chau ]
[ November 21, 2008: Message edited by: Benjamin Chau ]

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
The nextDouble() method will throw an InputMismatchException if the next token is not a double. Have you learned about exceptions yet?

There's also a "hasNextDouble()", which returns true if calling nextDouble() will work (i.e., won't throw an exception.)

Note that you can't compare Strings with "==" this way; use the equals() method instead:

Rob Spoor
Sheriff
Posts: 20511
54
Originally posted by Ernest Friedman-Hill:

Make that

Saves you the creation of the upper case string.

Benjamin Chau
Ranch Hand
Posts: 49
Originally posted by Ernest Friedman-Hill:
There's also a "hasNextDouble()", which returns true if calling nextDouble() will work (i.e., won't throw an exception.)

Ernest, thanks for your help! We haven't covered exceptions yet being this is an intro. class. I looked up the hasNextDouble() method you suggested. It looks like something I can definitely use but the textbook was vague on it. Is it a method or a boolean variable. If it's a boolean I'd have to declare it as such before using but the "()" indicates it's a method???

Here's the section of code in question that I modified with hasNextDouble().

When I tried to compile, I got 67 errors. I'm surely needing more help. Thanks!

Benjamin

[ November 22, 2008: Message edited by: Benjamin Chau ]
New lines to avoid very long lines. CR[/edit]
[ November 22, 2008: Message edited by: Campbell Ritchie ]

Benjamin Chau
Ranch Hand
Posts: 49
Originally posted by Rob Prime:

Make that

Saves you the creation of the upper case string.

Rob, thanks for your help! I like that syntax a lot better than what I was using. Simplified is good. I'm still getting buckoo errors when I try to compile but I will post the working code when I have it. Thanks again!

Benjamin

Campbell Ritchie
Sheriff
Posts: 48652
56
You need to revise the syntax for an "if" statement. Look here. You need to avoid == true and == false, because you can unintentionally write = true or = false and get nasty errors later on.
If you want it to be false you just need a ! and probably another pair of ().

Benjamin Chau
Ranch Hand
Posts: 49
Thanks Campbell, I revised. Got a few more questions about this program if anyone can help, that would be greatly appreciated...

1. What's the syntax to make the display extend accuracy on decimal numbers? For instance I want to display \$5.50 instead of \$5.5.
2. I still don't have syntax to check if the user enters valid characters for the amountTendered and amountOwed. Any suggestions anyone? I think I still need to use an if...else statement.
3. In the last statement of my do...while loop, I want to also cover the possibility of the user just entering a "Y" or "y" instead of a "Yes" answer. Can I add on arguments somehow. I tried this but it didn't work:

Regardless, here's the code that works. I just want to refine the program a bit more.

Campbell Ritchie
Sheriff
Posts: 48652
56
Originally posted by Benjamin Chau:
1. What's the syntax to make the display extend accuracy on decimal numbers? For instance I want to display \$5.50 instead of \$5.5.
Go through the Javaï¿½ Tutorials and see if you can find anything about "formatting numeric print output." There are more details in the Formatter class.
Originally posted by Benjamin Chau:
2. I still don't have syntax to check if the user enters valid characters for the amountTendered and amountOwed. Any suggestions anyone? I think I still need to use an if...else statement.
That is particularly awkward at your stage; you have neither learnt about regular expressions nor about Exceptions which could both be used to validate input or check for error conditions, so at your stage it might be better to trust that you will enter valid inputs.
Originally posted by Benjamin Chau:

3. In the last statement of my do...while loop, I want to also cover the possibility of the user just entering a "Y" or "y" instead of a "Yes" answer. Can I add on arguments somehow. I tried this but it didn't work: