I have been given an assignment to write a method that takes a monetary value less than $1000.00 and writes the word equivalent of that amount. This amount needs to include the part after the decimal as well (essentially the change part of the amount).
i.e.
Enter·check·amount:567.89↵
five·hundred·sixty·seven·and·89/100↵
I see that the topic has been covered numerous times but I can't seem to find any information on what I'm requesting. I am new to this (4 weeks) so forgive me if I'm asking the wrong questions or stating what I've done in the wrong manner. I'm able to output the amount in words (as you can see from my result) but I believe the issue I am having lies somewhere in the user entering a Double and my code not including anything requesting <1000.00. My main method does include the double number but each time I try to change the Int type in the moneyWord method to a Double the number Int in my If statements states that an int cannot be converted to a Double(which I know). Also, I think my return statement may needs to include more information. Again, I may be looking in the wrong direction for the error but I believe this is where I'm going wrong. In addition, I think some of this material may be taught in future classes as I had to look into chapters in my textbook that have not yet been covered - namely Arrays. Here is my code. Any assistance would be appreciated
//program to write the word equivalent of a check amount
import java.util.Scanner;
public class CheckToWord {
public static void main(String[] args) { // main method
double number = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the check amount:"); // prompt user to enter check amount
number = scanner.nextDouble();
if (number == 0) {
System.out.print("Zero");
} else {
System.out.print("" + moneyWord((int) number)); // output amount in words
}
}
private static String moneyWord(int number) {
String words = ""; // variable to hold string representation of number
String onesArray[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
String tensArray[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
"ninety" };
if (number < 0) { // convert the number to a string
String numberStr = "" + number;
numberStr = numberStr.substring(1); // remove minus before the number
return "minus " + moneyWord((int) Double.parseDouble(numberStr)); // add minus before the number and convert
// the rest of number
}
if ((number / 1000) > 0) { // check if number is divisible by 1 thousand
words += moneyWord(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0) { // check if number is divisible by a hundred
words += moneyWord(number / 100) + " hundred ";
number %= 100;
}
if (number < 20) { // check if number is within the teens
words += onesArray[number]; // get the appropriate value from ones array
} else {
words += tensArray[number / 10]; // get the appropriate value from tens array
if ((number % 10) > 0) {
words += "-" + onesArray[number % 10];
}
}
return words;
}
}
Result
Please enter the check amount:
1523.23
one thousand five hundred twenty-three
Try this:
public class CheckToWord {
public static void main(String[] args) { //main method
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the check amount:"); //prompt user to enter check amount
double number = scanner.nextDouble();
String[] parsed = parse(Double.toString(number));
int main = Integer.parseInt(parsed[0]);
int change = Integer.parseInt(parsed[1]);
if (main == 0 && change == 0) {
System.out.print("Zero");
} else {
String mwm = moneyWord(main);
String mwc = moneyWord(change);
System.out.println("" + mwm + " and " + mwc + " cents");
}
}
private static String[] parse(String number) {
String[] split = number.contains(".") ? number.split(Pattern.quote(".")) : new String[]{number, "00"};
String main = split[0];
String change = split[1].length() >= 2 ? split[1].substring(0, 2) :
split[1].length() == 1 ? split[1] + "0" : "00";
return new String[]{main, change};
}
private static String moneyWord(int number) {
if(number > 1000) {
throw new IllegalArgumentException("Number value should be less than 1000");
}
String words = ""; // variable to hold string representation of number
String onesArray[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
String tensArray[] = {"zero", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
if (number < 0) { // convert the number to a string
String numberStr = "" + number;
numberStr = numberStr.substring(1); // remove minus before the number
return "minus " + moneyWord((int) Double.parseDouble(numberStr)); // add minus before the number and convert the rest of number
}
if ((number / 1000) > 0) { // check if number is divisible by 1 thousand
words += moneyWord(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0) { // check if number is divisible by a hundred
words += moneyWord(number / 100) + " hundred ";
number %= 100;
}
if (number < 20) { // check if number is within the teens
words += onesArray[number]; // get the appropriate value from ones array
} else {
words += tensArray[number / 10]; // get the appropriate value from tens array
if ((number % 10) > 0) {
words += "-" + onesArray[number % 10];
}
}
return words;
}
}
Example output:
Please enter the check amount:
10.25
ten and twenty-five cents