patternjavaMinor
Writing the word equivalent of a check (cheque) amount
Viewed 0 times
theequivalentamountwritingchequewordcheck
Problem
I am looking for feedback on a solution to the following problem posed from a book that I'm working through (Java: How To Program 9th Edition):
Continuing the discussion in Exercise 16.20, we reiterate the
importance of designing check-writing systems to prevent alteration of
check amounts. One common security method requires that the amount be
written in numbers and spelled out in words as well. Even if someone
is able to alter the numerical amount of the check, it’s extremely
difficult to change the amount in words. Write an application that
inputs a numeric check amount that’s less than £1000 and writes the
word equivalent of the amount. For example, the amount 112.43 should
be written as
ONE hundred TWELVE and 43 pence
I'm interested in solutions which cover the areas I have studied so far, those being:
I have been a bit naughty by jumping ahead of the text to use a little recursion within the
I can't help feeling that the way I have implemented the
```
import java.util.Scanner;
public class WordCheckAmount {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
String
Continuing the discussion in Exercise 16.20, we reiterate the
importance of designing check-writing systems to prevent alteration of
check amounts. One common security method requires that the amount be
written in numbers and spelled out in words as well. Even if someone
is able to alter the numerical amount of the check, it’s extremely
difficult to change the amount in words. Write an application that
inputs a numeric check amount that’s less than £1000 and writes the
word equivalent of the amount. For example, the amount 112.43 should
be written as
ONE hundred TWELVE and 43 pence
I'm interested in solutions which cover the areas I have studied so far, those being:
- Introduction to Computers and Java
- Introduction to Java Applications
- Introduction to Classes, Objects, Methods and Strings
- Control Statements: Part 1
- Control Statements: Part 2
- Methods: A Deeper Look
- Arrays and ArrayLists
- Classes and Objects: A Deeper Look
- Object-Oriented Programming: Inheritance
- Object-Oriented Programming: Polymorphism
- Exception Handling: A Deeper Look
- GUI Components: Part 1 (Swing)
- Strings, Characters and Regular Expressions
I have been a bit naughty by jumping ahead of the text to use a little recursion within the
readMe() method. I figured from the description of recursion that the line of code in question would behave this way and I was right.I can't help feeling that the way I have implemented the
readMe() method is somewhat messy and I can see that it would not scale very well beyond £1000 or four digits. Is there a simpler way of doing this? All forms of feedback are welcome.```
import java.util.Scanner;
public class WordCheckAmount {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
String
Solution
Yes! Your code is more complicated than it needs to be.
Here is a method I came up with, that returns a string:
It is possible to improve even further to add thousands, millions, etc.
- You treat the String as a String instead of first parsing it as a number and treating it as a number. It is more complicated to treat chars than numbers when you want to use them as numbers.
- You have some code duplication near the logic of `if (testMe
- Instead of printing directly, it is better to return a String and let the caller print
Here is a method I came up with, that returns a string:
static String[] oneThruNineteen = { "NULL", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
"SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE",
"THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN",
"EIGHTEEN", "NINETEEN" };
static String[] theDecades = { "NULL", "NULL", "TWENTY", "THIRTY", "FOURTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY" };
public static String numToStr(int amount) {
String str = "";
if (amount >= 100) {
str += oneThruNineteen[ amount / 100] + " hundred ";
str += numToStr(amount % 100);
}
else if (amount >= 20) {
str += theDecades[ amount / 10 ];
str += numToStr(amount % 10);
}
else {
str += oneThruNineteen[ amount ];
}
return str;
}It is possible to improve even further to add thousands, millions, etc.
Code Snippets
static String[] oneThruNineteen = { "NULL", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
"SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE",
"THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN",
"EIGHTEEN", "NINETEEN" };
static String[] theDecades = { "NULL", "NULL", "TWENTY", "THIRTY", "FOURTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY" };
public static String numToStr(int amount) {
String str = "";
if (amount >= 100) {
str += oneThruNineteen[ amount / 100] + " hundred ";
str += numToStr(amount % 100);
}
else if (amount >= 20) {
str += theDecades[ amount / 10 ];
str += numToStr(amount % 10);
}
else {
str += oneThruNineteen[ amount ];
}
return str;
}Context
StackExchange Code Review Q#29457, answer score: 7
Revisions (0)
No revisions yet.