Hi, I am trying code to print a three digit number in words. But this isn't working if first two digits from right are between 11 & 19 (both including).
Any help?
package com;
import java.util.Stack;
public class TestMain {
public static void main(String[] args) {
Integer i=512;
int temp =i;int pos=1;
Stack<String> stack=new Stack<String>();
while(temp>0){
int rem=temp%10;
temp=temp/10;
if(rem!=0){stack.push(getString(rem, pos));}
pos*=10;
}
do{
System.out.print(stack.pop()+" ");
}while(!stack.isEmpty());
}
static String getString(int i,int position){
String str=null;
if(position==10){
i*=position;
}
switch(i){
case 1:
str= "One";break;
case 2:
str= "two";break;
case 3:
str= "three";break;
case 4:
str= "four";break;
case 5:
str= "five";break;
case 6:
str= "six";break;
case 7:
str= "seven";break;
case 8:
str= "eight";break;
case 9:
str= "nine";break;
case 10:
str= "Ten";break;
case 11:
str= "Eleven";break;
case 12:
str= "Twelve";break;
case 13:
str= "thirteen";break;
case 14:
str= "fourteen";break;
case 15:
str= "fifteen";break;
case 16:
str= "sixteen";break;
case 17:
str= "seventeen";break;
case 18:
str= "eighteen";break;
case 19:
str= "Nineteen"; break;
case 20:
str= "twenty";break;
case 30:
str= "Thirty";break;
case 40:
str= "forty";break;
case 50:
str= "Fifty";break;
case 60:
str= "sixty";break;
case 70:
str= "Seventy";break;
case 80:
str= "Eighty"; break;
case 90:
str= "Ninety";break;
case 100:
str= "Hundred";
break;
}
if(position>=100){
str=str+ " "+getString(position, 0);
}
return str;
}
}