Someone asked me to make a simple program for converting number to words , but no one came back to get it so decided to publish it out of trippings..
Heres the java code for it..play around and enjoy ( Net Beans 3.6 is an acceptable java IDE)
The Code
package NumConverter;
/* To make use of Dialog boxes in Input and Output*/
import javax.swing.JOptionPane;
public class NumCon {
/*Array members to be used in conversion*/
private String[] strOnes={""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
private String[] strSpecial={" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
private String[] strTensSpecial={""," Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
private String[] strPlaces={""," Tens"," Hundred"," Thousand"," Thousand"," Hundred"," Million"," Million"," Hundred"," Billion"};
public NumCon() {
}
public static void main(String[] args) {
/*instantiate a NumCon class*/
NumCon nC=new NumCon();
int iInput=0; //stores the integer input
String strInput=""; //stores the integer input converted to string
strInput=JOptionPane.showInputDialog("Enter the value :");
if(strInput.length()<=9 && strInput.length()>=1){
iInput=Integer.parseInt(strInput); //get the input
strInput=Integer.toString(iInput); //convert to string
JOptionPane.showMessageDialog(null,nC.genWords(strInput)); //generate the conversion
}
else JOptionPane.showMessageDialog(null,"Limited to 9 Digits and at least 1 Digit only.");
System.exit(0); //normally exit
}
private String genWords(String pstrVal){
char cArr[]=pstrVal.toCharArray();
String strW="";
int iPos=pstrVal.length()-1;
for(int i=0;i<pstrVal.length();i++){
char c=cArr[i];
if(iPos==0 && pstrVal.length()==1){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
}
if(iPos==1 || iPos==4 || iPos==7){
/*checks if the 2nd digit is one*/
if(c!="1"){
/*e.g. 51-- FIFTY + ONE */
strW+=strTensSpecial[Integer.parseInt(Character.toString(c))];
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i+1]))];
}
else{
/*e.g. 12-- TWELVE */
strW+=strSpecial[Integer.parseInt(Character.toString(cArr[i+1]))];
}
}
if(iPos==2 || iPos==5){
if(c!="0"){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
}
if(iPos==3){
if(pstrVal.length()==4){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else{
if((Integer.parseInt(Character.toString(cArr[i])) +Integer.parseInt(Character.toString(cArr[i+1]))+Integer.parseInt(Character.toString(cArr[i+2])))!=0)
strW+=strPlaces[iPos];
}
}
if(iPos==6){
if(pstrVal.length()==7){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
if(iPos==8 ){
if(pstrVal.length()==9){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
iPos--;
}
if(strW.trim().length()==0) strW="Zero";
return strW;
}
}
Note: The codes are not properly aligned because of the publishing..please copy the code and do the restructuring to make the code more readable and easier to debug.
Heres the java code for it..play around and enjoy ( Net Beans 3.6 is an acceptable java IDE)
The Code
package NumConverter;
/* To make use of Dialog boxes in Input and Output*/
import javax.swing.JOptionPane;
public class NumCon {
/*Array members to be used in conversion*/
private String[] strOnes={""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
private String[] strSpecial={" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
private String[] strTensSpecial={""," Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
private String[] strPlaces={""," Tens"," Hundred"," Thousand"," Thousand"," Hundred"," Million"," Million"," Hundred"," Billion"};
public NumCon() {
}
public static void main(String[] args) {
/*instantiate a NumCon class*/
NumCon nC=new NumCon();
int iInput=0; //stores the integer input
String strInput=""; //stores the integer input converted to string
strInput=JOptionPane.showInputDialog("Enter the value :");
if(strInput.length()<=9 && strInput.length()>=1){
iInput=Integer.parseInt(strInput); //get the input
strInput=Integer.toString(iInput); //convert to string
JOptionPane.showMessageDialog(null,nC.genWords(strInput)); //generate the conversion
}
else JOptionPane.showMessageDialog(null,"Limited to 9 Digits and at least 1 Digit only.");
System.exit(0); //normally exit
}
private String genWords(String pstrVal){
char cArr[]=pstrVal.toCharArray();
String strW="";
int iPos=pstrVal.length()-1;
for(int i=0;i<pstrVal.length();i++){
char c=cArr[i];
if(iPos==0 && pstrVal.length()==1){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
}
if(iPos==1 || iPos==4 || iPos==7){
/*checks if the 2nd digit is one*/
if(c!="1"){
/*e.g. 51-- FIFTY + ONE */
strW+=strTensSpecial[Integer.parseInt(Character.toString(c))];
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i+1]))];
}
else{
/*e.g. 12-- TWELVE */
strW+=strSpecial[Integer.parseInt(Character.toString(cArr[i+1]))];
}
}
if(iPos==2 || iPos==5){
if(c!="0"){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
}
if(iPos==3){
if(pstrVal.length()==4){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else{
if((Integer.parseInt(Character.toString(cArr[i])) +Integer.parseInt(Character.toString(cArr[i+1]))+Integer.parseInt(Character.toString(cArr[i+2])))!=0)
strW+=strPlaces[iPos];
}
}
if(iPos==6){
if(pstrVal.length()==7){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
if(iPos==8 ){
if(pstrVal.length()==9){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
iPos--;
}
if(strW.trim().length()==0) strW="Zero";
return strW;
}
}
Note: The codes are not properly aligned because of the publishing..please copy the code and do the restructuring to make the code more readable and easier to debug.
Comments