aspose file tools*
The moose likes Beginning Java and the fly likes Change Due/Tendered Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Change Due/Tendered Program" Watch "Change Due/Tendered Program" New topic
Author

Change Due/Tendered Program

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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:



[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Ernest Friedman-Hill:

Make that

Saves you the creation of the upper case string.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
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 ]
[edit]New lines to avoid very long lines. CR[/edit]
[ November 22, 2008: Message edited by: Campbell Ritchie ]
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Oct 05, 2008
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:

while (answer.equalsIgnoreCase("YES" || "Y"))

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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:

while (answer.equalsIgnoreCase("YES" || "Y"))
Back to the Java� Tutorials, where you find that the || operator can only be applied to boolean values (or probably in Java5 and Java6) to Booleans. So what is to the left of the || has to be a boolean, not a String, and what is on the right of the || has to be a boolean. You have already seen that "answer.equalsIgnoreCase("YES")" is a boolean expression, so . . .

Good luck with it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Change Due/Tendered Program