i want to create a program that would translate number to words from 1-1 billion
this is how i started:

package app.runner;


import javax.swing.*;
public class EnglishNumberToWords{
    public static void main(String args[]){
        int inputNumber=0;
        String oness[]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        String tens[]={"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        String answer="";
        String calculate="";

        inputNumber=Integer.parseInt(JOptionPane.showInputDialog(null,"PLease Enter a Number: "));

        int billion=inputNumber/1000000;
        int billion2=inputNumber%1000000;
        int billion3=billion*1000000;

        int million=billion2/100000;
        int million2=billion2%100000;
        int million3=million*100000;


        int libo = million2/ 1000;
        int dalawalibo =million2 % 1000;
        int tatlolibo=  libo * 1000;

        int daan = dalawalibo / 100;
        int dalawadaan = dalawalibo % 100;
        int tatlodaan=  daan * 100;


        int ones=dalawadaan/10;
        int isa2=dalawadaan%10;
        int isa1= ones*10;

        if(inputNumber >=0 && inputNumber<=19){
            calculate=oness[inputNumber-1];
            if(inputNumber>=10 && inputNumber<=19){
                calculate=oness[inputNumber-1];
            }
        }

        else if(inputNumber >=20 && inputNumber<=90){

            calculate=tens[isa1];
        }
        JOptionPane.showMessageDialog(null,calculate);

    }
}

it says array out of bounds exception at line number 45
please guys check my work.. :)
thank you

Recommended Answers

All 8 Replies

Looks like isa1 has a wrong value. Use print statements throughout your code to print the key variables so you can see where it's going wrong.

Well.It takes time to be learned,But i must...

May be u get some idea from here....

class constNumtoLetter
  {
      String[] unitdo ={"", " One", " Two", " Three", " Four", " Five",
         " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve",
         " Thirteen", " Fourteen", " Fifteen",  " Sixteen", " Seventeen", 
         " Eighteen", " Nineteen"};
      String[] tens =  {"", "Ten", " Twenty", " Thirty", " Forty", " Fifty",
         " Sixty", " Seventy", " Eighty"," Ninety"};
      String[] digit = {"", " Hundred", " Thousand", " Lakh", " Crore"};
     int r;


      //Count the number of digits in the input number
      int numberCount(int num)
      {
          int cnt=0;

          while (num>0)
          {
            r = num%10;
            cnt++;
            num = num / 10;
          }

            return cnt;
      }


      //Function for Conversion of two digit

      String twonum(int numq)
      {
           int numr, nq;
           String ltr=\"";

           nq = numq / 10;
           numr = numq % 10;

           if (numq>19)
             {
           ltr=ltr+tens[nq]+unitdo[numr];
             }
           else
             {
           ltr = ltr+unitdo[numq];
             }

           return ltr;
      }

      //Function for Conversion of three digit

      String threenum(int numq)
      {
             int numr, nq;
             String ltr = "";

             nq = numq / 100;
             numr = numq % 100;

             if (numr == 0)
              {
              ltr = ltr + unitdo[nq]+digit[1];
               }
             else
              {
              ltr = ltr +unitdo[nq]+digit[1]+" and"+twonum(numr);
              }
             return ltr;

      }

}

 class originalNumToLetter

   {

      public static void main(String[] args) throws Exception
      {

          //Defining variables q is quotient, r is remainder

          int len, q=0, r=0;
          String ltr = " ";
          String Str = "Rupees";
          constNumtoLetter n = new constNumtoLetter();
          int num = Integer.parseInt(args[0]);

          if (num <= 0) System.out.println(\"Zero or Negative number not for conversion");

          while (num>0)
          {

             len = n.numberCount(num);

             //Take the length of the number and do letter conversion

             switch (len)

             {
                  case 8:
                          q=num/10000000;
                          r=num%10000000;
                          ltr = n.twonum(q);
                          Str = Str+ltr+n.digit[4];
                          num = r;
                          break;

                  case 7:
                  case 6:
                          q=num/100000;
                          r=num%100000;
                          ltr = n.twonum(q);
                          Str = Str+ltr+n.digit[3];
                          num = r;
                          break;

                  case 5:
                  case 4:

                           q=num/1000;
                           r=num%1000;
                           ltr = n.twonum(q);
                           Str= Str+ltr+n.digit[2];
                           num = r;
                           break;

                  case 3:


                            if (len == 3)
                                r = num;
                            ltr = n.threenum(r);
                            Str = Str + ltr;
                            num = 0;
                            break;

                  case 2:

                           ltr = n.twonum(num);
                           Str = Str + ltr;
                           num=0;
                           break;

                  case 1:
                           Str = Str + n.unitdo[num];
                           num=0;
                           break;
                  default:

                          num=0;
                          System.out.println(\"Exceeding Crore....No conversion");
                          System.exit(1);


              }
                          if (num==0)
                          System.out.println(Str+\" Only");
            }

         }

      }

i think im encountering errors on your code there are things that i do not understand
if (num <= 0) System.out.println(\"Zero or Negative number not for conversion");`
whats the purpose of "\" on the println?

It's a mistake.
ps: You should be writing your own rather than copying someone else's - that's not an especially good version of this application anyway.

yes im not copying it :) im just trying to test it and i do not copy codes i figured that its better to get a failing grade tan to have high grades without good effort that why i noticed that there were errors on the code because i tried to run it on eclipse!

OK. That's good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.