What shall I do to this program? It does not output correct.
When I input "6412" it will just output "Six Thousand Four Hundred Two"
When I input "17" It will only output "Ten Seven"
When I input "6433" it will just output "Six Thousand Four Hundred Three"
Why is it so? It supposedly be like: "17" should be "Seventeen", "12" should be "Twelve", "33" should be "Thirty Three" but still does not output correct.
Here is my code:
import java.io.*; public class NewClass { public static void main(String[]args) throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); String o="Y"; String x; do {String ans="",onez="",tenz="",hundredz="",thousandz=""; try { System.out.println("Enter a number from 1 to 9999:"); x=input.readLine(); int num=Integer.parseInt(x); int thousands=num/1000; int hundreds=(num/100)%10; int tens=(num-(hundreds*100))/10; int one=num%10; if (num>=1&&num<=9999) { switch(thousands){case 1:thousandz="One Thousand ";break; case 2:thousandz="Two Thousand ";break; case 3:thousandz="Three Thousand ";break; case 4:thousandz="Four Thousand ";break; case 5:thousandz="Five Thousand ";break; case 6:thousandz="Six Thousand ";break; case 7:thousandz="Seven Thousand ";break; case 8:thousandz="Eight Thousand ";break; case 9:thousandz="Nine Thousand ";break; } switch(hundreds) {case 1: hundredz="One Hundred ";break; case 2: hundredz="Two Hundred ";break; case 3: hundredz="Three Hundred ";break; case 4: hundredz="Four Hundred ";break; case 5: hundredz="Five Hundred ";break; case 6: hundredz="Six Hundred ";break; case 7: hundredz="Seven Hundred ";break; case 8: hundredz="Eight Hundred ";break; case 9: hundredz="Nine Hundred ";break;} switch(tens) {case 1:tenz="Ten ";break; case 2:tenz="Twenty ";break; case 3:tenz="Thirty ";break; case 4:tenz="Forty ";break; case 5:tenz="Fifty ";break; case 6:tenz="Sixty ";break; case 7:tenz="Seventy ";break; case 8:tenz="Eigthty ";break; case 9:tenz="Ninety ";break; case 11:tenz="Eleven";break; case 12:tenz="Twelve";break; case 13:tenz="Thirteen";break; case 14:tenz="Fourteen";break; case 15:tenz="Fifteen";break; case 16:tenz="Sixteen";break; case 17:tenz="Seventeen";break; case 18:tenz="Eighteen";break; case 19:tenz="Nineteen";break; } switch(one) {case 1:onez="One";break; case 2:onez="Two";break; case 3:onez="Three";break; case 4:onez="Four";break; case 5:onez="Five";break; case 6:onez="Six";break; case 7:onez="Seven";break; case 8:onez="Eight";break; case 9:onez="Nine";break; } ans=thousandz+hundredz+tenz+onez; System.out.println("Your input is "+ans); System.out.println("Continue?"); System.out.println("Press Y if YES, N if NO."); o=input.readLine(); } else { System.out.println("invalid range"); } } catch(NumberFormatException ae) { System.out.println("invalid input"); System.out.println("enter another?"); System.out.println("Press Y if YES, N if NO.");; o = input.readLine(); } } while(o.equals ("y") || o.equals ("Y")); } }