Write a script that inputs a numeric check amount and writes the word equivalent
ID: 3561191 • Letter: W
Question
Write a script that inputs a numeric check amount and writes the word equivalent of the
amount. For example, the amount 112.43 should be written as
ONE HUNDRED TWELVE and 43/100.
Explanation / Answer
this program is for amount less than 100000 import java.util.Scanner; public class WordCheckAmount { public static void main(String[] args) { Scanner sc = new Scanner( System.in ); String userInput; boolean correctInput = false; do { System.out.println( "Please enter an amount below 100000" ); userInput = sc.nextLine(); System.out.println(); if( userInput.indexOf( '.' ) < 0 && validateAmount( userInput ) ) // no decimal point { correctInput = true; readMe( userInput ); } if( userInput.indexOf( '.' ) > 0 && validatePointAmount( userInput ) ) // decimal point { correctInput = true; String[] tokens = userInput.split( "\." ); readMe( tokens[ 0 ] ); System.out.printf( "and " ); readMe( tokens[ 1 ] ); System.out.println( "pence" ); } }while( !correctInput ); } public static boolean validatePointAmount( String amount ) { return amount.matches( "\d{0,3}.?\d{0,2}" ); } public static boolean validateAmount( String amount ) { return amount.matches( "\d{0,3}" ); } public static void readMe( String amount ) { String[] "NULL", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; String[] theDecades = { "NULL", "NULL", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; int theLength = amount.length(); switch( theLength ) { case 3: // 3 digits System.out.printf( "%s ", oneThruNineteen[ Character.getNumericValue( amount.charAt( 0 ) ) ] ); System.out.printf( "hundred " ); int testMe = Character.getNumericValue( amount.charAt( 1 ) ); if( testMe < 2 ) // if tens unit in the teens System.out.printf( "%s ", oneThruNineteen[ Integer.parseInt( amount.substring( 1, 3 ) ) ] ); else // if tens unit twenty or higher { String feedMe = amount.substring( 1 , 3 ); readMe( feedMe ); // recursion I presume? feedback for 2 digits } break; case 2: // 2 digits int testMe2 = Character.getNumericValue( amount.charAt( 0 ) ); if( testMe2 < 2 ) // if tens unit in the teens System.out.printf( "%s ", oneThruNineteen[ Integer.parseInt( amount ) ] ); else // if tens unit twenty or higher { System.out.printf( "%s ", theDecades[ Character.getNumericValue( amount.charAt( 0 ) ) ] ); int testMe3 = Character.getNumericValue( amount.charAt( 1 ) ); if( testMe3 > 0 ) // if next digit is higher than 0, otherwise you get TWENTY NULL for 20 etc System.out.printf( "%s ", oneThruNineteen[ Character.getNumericValue( amount.charAt( 1 ) ) ] ); } break; case 1: // just the 1 digit System.out.printf( "%s ", oneThruNineteen[ Integer.parseInt( amount ) ] ); break; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.