As usual, I'm just another highschooler desperate enough to resort to asking the question after lots of googling.
I'm working on one of them programs that can somewhat be applied in real life (but never are). In this case, a change counter that sums up the number of dimes, nickels, quarters and remaining cents and shows them in one sentence.
My problem is, I need to make a proper sentence format that uses commas and an "and" in the end, which I can't manage with my very limited current knowledge.
The part of my program concerned with what I want to ask is this:
System.out.print(backupInput + ": ");
// Outputting the quarters
if (quarters > 0)
{
if (quarters == 1)
System.out.print("one quarter, ");
else
System.out.print(quarters + " quarters, ");
}
// Outputting the dimes
if (dimes > 0)
{
if (dimes == 1)
System.out.print("one dime, ");
else
System.out.print(dimes + " dimes, ");
}
// Outputting the nickels
if (nickels > 0)
{
if (nickels == 1)
System.out.print("one nickel, ");
else
System.out.print(nickels + " nickels ");
}
// Outputting the cents
if (cents > 0)
{
if (cents == 1)
System.out.print(" one cent");
else
System.out.print(cents + " cents ");
}
The problem is, anything this outputs give the result of something like
7: 1 dime, 2 cents
While it's supposed to be, according to the example format given:
7: 1 dime and 2 cents
65: 2 quarters, 1 dime and one nickel
66: 2 quarters, 1 dime, 1 nickel, and one cent
Keep in mind I tried nearly everything I could think of and in all my tries it was too inefficient and didn't even work in all the cases. I'm not trying to save myself from work here. I just want to learn the concept behind doing it right.
That's about it. The rest of the program works fine and dandy. If any of you could help me, please keep the answer easy to understand for a newbie like me so it's easier to grasp the concept :) Thanks!
Sometimes it's easier to break the problem down a bit. What I'd probably do is create an array of strings: ["2 quarters", "1 dime", "1 nickel", "one cent"]
Then look at how many (valid) array elements I had and loop through N-1 of the N elements, outputting the lines separated by commas. Then (assuming that there's more than one array element) output "and" followed by the last array element.
There are other ways that are "more efficient", but I've been in this biz 40 years, and I'll take "easy to understand and works" over "efficient" any day.
(Of course, this isn't the only way to skin this cat, and probably there are others that are equally simple, but that's for you and others to invent.)
(Note that it's critical to avoid code that must decide, between everyone of your if
statements, whether or not to insert "and". When you repeat the same logic over and over again you tend to multiply your errors and create a maintenance nightmare.)
Your current approach is to calculate the count for a coin type and then immediately print the result.
If instead, you first compute the counts for all the coins without any printing, you can then see which is the smallest coin with a nonzero count (and thus the last to be named in the output). Using that, I think you can figure out how to glue the counts together and print the desired text.
Since you are learning Java, I won't give you the full solution, but here are some directions:
String
variable instead of System.out.print
ing it bit by bitString
, remove the trailing comma, if any (only in the cases cents == 0
or nickels > 1
, but the latter may be a mistake of yours)" and"
, if anyString
If you are still totally stuck, let us know, I'm sure someone will come up with a ready-made solution (if not already).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With