Results 1 to 3 of 3
  1. #1
    miatech is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default need help converting numbers to word

    guys, I wrote this program that converts numbers to words eq: 4914 to "four thousand and nine hundred and fourteen"; however, I'm having problems with the toString method. Is not printing correctly some numbers. the functions and their return type can't be changed. I need help with the toString method that actually puts all the words together. I appreciate any help.
    Java Code:
    package numberstowords;
     
    import java.util.*;
     
    public class Numbers {
     
      
     
        //getting single digit number
        private int getOnes()
        {
            one = value % 10;
            return one;
        }
     
        //getting tens numbers
        private int getTens()
        {
            ten = value % 100;
            ten /= 10;
            return ten;
        }
     
        //getting hundreds numbers
        private int getHundreds()
        {
            hundred = value % 1000;
            hundred /= 100;
            return hundred;
        }
     
        //getting thousands numbers
        private int getThousands()
        {
            thousand = value % 10000;
            thousand /= 1000;
            return thousand;
        }
     
        //converting and returning string of ones 
        private String digitsToString(int one)
        {
            return SINGLE_DIGITS_WORDS[one];        
        }
     
        //converting and returning strings of tens and teens
        private String tensAndOnesToString(int ten, int one)
        {
            if(ten == 1)//if number is a teen return, else return tens 
            { 
                return TEEN_DIGITS_WORDS[one];
            }
            return TEN_DIGITS_WORDS[ten-2];         
        }
     
        //converting and returning strings of hundreds
        private String hundredsToString(int hundred)
        {
            return digitsToString(hundred) + " hundred";
        }
     
        private String thousandsToString(int thousand)
        {
            return digitsToString(thousand) + " thousand";
        }
     
        @Override
        public String toString()
        
      }
    tester

    Java Code:
    package numberstowords;
     
    import java.util.Scanner;
     
    public class Test {
     
        //array to store the word equivalent of numbers
        public static String[] s = new String[8];
     
        public static void main(String[] args) {
     
            int count = 0;    
            Scanner input = new Scanner(System.in);
            do
            {
                System.out.println("Enter an integer (4 digits max)--> ");
                int value = input.nextInt();
                if(value > 0)
                {
                    Numbers n = new Numbers(value);
                    //store n.toString in an arrayList s
                    //s[count] = n.toString();
                    System.out.println(n.toString());
                    count++;
                }
                else break;
            }while(true);
    Last edited by miatech; 05-15-2012 at 05:04 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: need help converting numbers to word

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: need help converting numbers to word

    Quoted for posterity

    db
    Quote Originally Posted by miatech View Post
    guys, I wrote this program that converts numbers to words eq: 4914 to "four thousand and nine hundred and fourteen"; however, I'm having problems with the toString method. Is not printing correctly some numbers. the functions and their return type can't be changed. I need help with the toString method that actually puts all the words together. I appreciate any help.
    Java Code:
    package numberstowords;
     
    import java.util.*;
     
    public class Numbers {
     
        //array containing single digits words numbers:0-9
        private final String[] SINGLE_DIGITS_WORDS;
     
        //array containing special words words eg:10-19
        private final String[] TEEN_DIGITS_WORDS;
     
        //array containing tens words numbers:20-90
        private final String[] TEN_DIGITS_WORDS;
     
        private int value,   //number to be converted to words
                    one,     //number to store digits
                    ten,     //number to store tens
                    hundred, //number to store hundred
                    thousand;//number to store thousand
     
        private String strOut;
     
        //conscructor: Initializing value and arrays
        public Numbers(int n)
        {
            value = n;
            SINGLE_DIGITS_WORDS = new String[]{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
            TEEN_DIGITS_WORDS = new String[]{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
            TEN_DIGITS_WORDS = new String[]{"twenty", "therty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        }
     
        //getting single digit number
        private int getOnes()
        {
            one = value % 10;
            return one;
        }
     
        //getting tens numbers
        private int getTens()
        {
            ten = value % 100;
            ten /= 10;
            return ten;
        }
     
        //getting hundreds numbers
        private int getHundreds()
        {
            hundred = value % 1000;
            hundred /= 100;
            return hundred;
        }
     
        //getting thousands numbers
        private int getThousands()
        {
            thousand = value % 10000;
            thousand /= 1000;
            return thousand;
        }
     
        //converting and returning string of ones 
        private String digitsToString(int one)
        {
            return SINGLE_DIGITS_WORDS[one];        
        }
     
        //converting and returning strings of tens and teens
        private String tensAndOnesToString(int ten, int one)
        {
            if(ten == 1)//if number is a teen return, else return tens 
            { 
                return TEEN_DIGITS_WORDS[one];
            }
            return TEN_DIGITS_WORDS[ten-2];         
        }
     
        //converting and returning strings of hundreds
        private String hundredsToString(int hundred)
        {
            return digitsToString(hundred) + " hundred";
        }
     
        private String thousandsToString(int thousand)
        {
            return digitsToString(thousand) + " thousand";
        }
     
        @Override
        public String toString()
        {
            int d1 = getThousands();
            int d2 = getHundreds();
            int d3 = getTens();
            int d4 = getOnes();
     
            strOut = "";       
     
            if(d1 != 0)
            {
                strOut = thousandsToString(thousand);
            }
     
            if(d2 != 0)
            {
                if(strOut.equals(""))
                    strOut = hundredsToString(hundred);
                else
                    strOut = strOut + " and " + hundredsToString(hundred);
            }
     
            if(d3 != 0)
            {
                if (strOut.equals(""))
                {
                    strOut = tensAndOnesToString(ten, one); 
     
                }
     
                else 
                {
                    strOut = strOut + " and " + tensAndOnesToString(ten, one);    
                }
     
            }
     
            if(d4 != 0)
     
            {
                if(strOut.equals(""))
                    strOut = digitsToString(one);
                else
                {
                    strOut = strOut +" "+ digitsToString(one);
     
                }
     
            }
     
            return strOut.substring(0, 1).toUpperCase() + strOut.substring(1);
     
        }
      }
    tester

    Java Code:
    package numberstowords;
     
    import java.util.Scanner;
     
    public class Test {
     
        //array to store the word equivalent of numbers
        public static String[] s = new String[8];
     
        public static void main(String[] args) {
     
            int count = 0;    
            Scanner input = new Scanner(System.in);
            do
            {
                System.out.println("Enter an integer (4 digits max)--> ");
                int value = input.nextInt();
                if(value > 0)
                {
                    Numbers n = new Numbers(value);
                    //store n.toString in an arrayList s
                    //s[count] = n.toString();
                    System.out.println(n.toString());
                    count++;
                }
                else break;
            }while(true);
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how to change numbers into word numbers?
    By akeni in forum New To Java
    Replies: 13
    Last Post: 11-18-2011, 08:46 AM
  2. Converting numbers to words
    By Zora in forum New To Java
    Replies: 2
    Last Post: 11-14-2011, 10:10 AM
  3. converting doc(ms word) to pdf
    By sgl in forum New To Java
    Replies: 0
    Last Post: 10-21-2010, 07:03 PM
  4. converting .pdf to .doc(microsoft word)format
    By ashik03 in forum Java Applets
    Replies: 1
    Last Post: 01-30-2010, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •