Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,421 people online right now. Registration is fast and FREE... Join Now!




How can to change an input of number in words to integers..

 
Reply to this topicStart new topic

How can to change an input of number in words to integers.., Using the program JCreator

Pulse
29 Jun, 2007 - 05:38 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2007
Posts: 3


My Contributions
Me and my groupmates want to create a program, wherein you input numbers expressed in words like "nine hundred ninety thousand one hundred and fifty" into numerical units like "990,100.50" including the decimals. and wherein a message will show up indicating an error in the input words if ever the users will try to put other words aside from numbered words. We started making this code all day but can't seem to make it work efficiently.

CODE
import javax.swing.*;
import java.util.*;
public class numberWords
{
        private static String [] num_words={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
        private static int [] num={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};

        private static int [] n;
        private static int [] arr;
        private static double and=0.00;
        private static int k,j;
    private static void method1()
    {
        int length1;
        System.out.println(n.length);
        length1=n.length;
        int v=-1;
        long sum1=0,sum=0,product=0,product2=0,the_number=0,inter_product=0;
        do
        {
            do
            {
                v++;
                if(v!=n.length)
                {
                    if((n[v]>=0)&&(n[v]<=99))
                    {
                        if(v+1!=n.length)
                        {
                            if((n[v+1])==100)
                            {
                                if((v+2!=n.length)&&(n[v+2]>=0)&&(n[v+2]<=99))
                                {
                                    if((v+3!=n.length)&&(n[v+3]>=0)&&(n[v+3]<=99))
                                    {
                                        if((v+4!=n.length)&&(n[v+4]>=0)&&(n[v+4]<=99))
                                        {

                                        }

                                        inter_product=(long)((n[v]*100)+n[v+2]+n[v+3]);
                                        sum1=(long)(sum1+inter_product);
                                        v=v+3;
                                        break;
                                    }
                                    inter_product=(long)((n[v]*100)+n[v+2]);
                                    sum1=(long)(sum1+inter_product);
                                    v=v+2;
                                    break;
                                }
                                inter_product=(long)(n[v]*100);
                                sum1=(long)(sum1+inter_product);
                                v=v+1;
                                break;
                            }

                            if((n[v+1]==1000)||(n[v+1]==1000000)||(n[v+1]==1000000000))
                            {
                                if (inter_product!=0)
                                {
                                    product2=(long)(inter_product*n[v+1]);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                                else
                                {
                                    product2=(long)(n[v]*n[v+1]);
                                    sum=(long)(sum+product2);
                                    v=v+1;
                                    break;
                                }
                            }
                            else
                            {

                                break;
                            }
                        }
                        sum=(long)(sum+n[v]);
                        break;
                    }
                    else
                    {
                        switch(n[v])
                        {
                            case 1000:
                                if (inter_product!=0)
                                {
                                    product2=(long)(inter_product*1000);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                                else
                                {
                                    product2=(long)(n[v-1]*1000);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                            case 1000000:
                                if (inter_product!=0)
                                 {
                                    product2=(long)(inter_product*1000000);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                                else
                                {
                                    product2=(long)(n[v-1]*1000000);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                            case 1000000000:
                                if (inter_product!=0)
                                  {
                                    product2=(long)(inter_product*n[v]);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                                else
                                {
                                    product2=(long)(n[v-1]*n[v]);
                                    sum=(long)(sum+product2);
                                    break;
                                }
                            default:
                                break;
                        }
                        break;
                    }
                }
                else
                {
                    break;
                }
            }while(v<n.length);
        }while(v<n.length);

        System.out.println("The number is: "+sum+"\nthe inter_product is: "+inter_product);
        JOptionPane.showMessageDialog(null,"The number is: "+sum+"\nthe inter_product is: "+inter_product,"Results",JOptionPane.INFORMATION_MESSAGE);
    }

    private static int choice(String check,int j1)
    {
        if(check.equalsIgnoreCase("billion"))
        {
            multiply(1000000000,j1);
            j1++;
            return j1;
        }
        else{
             if(check.equalsIgnoreCase("million"))
             {
                multiply(1000000,j1);
                j1++;
                return j1;
             }else{
                     if(check.equalsIgnoreCase("thousand"))
                     {
                        multiply(1000,j1);
                        j1++;
                        return j1;
                     }else{
                          if(check.equalsIgnoreCase("hundred"))
                          {
                            multiply(100,j1);
                            j1++;
                            return j1;
                          }else{
                            return j1;
                          }
                     }
             }
        }
    }

    private static void point(String [] array21)
    {
        int var=0;
        j++;
        k=-1;
        do
        {
            k++;
            do
            {
                if((j!=array21.length)&&(k!=num_words.length))
                {
                    if(array21[j].equalsIgnoreCase(num_words[k]))
                    {
                        var=(num[k]*10);
                        k=-1;
                        do
                        {
                            k++;
                            if(((j+1)!=array21.length)&&(k!=num_words.length))
                            {
                                if(array21[j+1].equalsIgnoreCase(num_words[k]))
                                {
                                    var=var+num[k];
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }while(k<num_words.length);
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }while(k<num_words.length);

        }while(k<num_words.length);
        if(var==0)
        {
            JOptionPane.showMessageDialog(null,"Sorry! You did not enter a numbered word!","RESULT!",JOptionPane.INFORMATION_MESSAGE);

        }
        System.out.println("result: "+var);
    }
    private static void multiply(int number, int number1)
    {
        int second,third;
        second=number;
        third=number1+1;
        send(second,third);
    }

    private static void send(int num1,int index1)
    {
        int index;
        int use;
        index=index1-1;
        if(n[index]!=0)
            n[index+1]=num1;
        else
            n[index]=num1;
        System.out.println(n[index]+" \n:"+n.length+"\nindex is : "+index);

    }

    public static void main(String args[])
    {
        int i,q,first=0;

        String input;

        input=JOptionPane.showInputDialog("Enter a number in words: ");
        StringTokenizer string1=new StringTokenizer(input);

        //counts the tokens then assigns it as the length of the array

        String [] tokens = new String[string1.countTokens()];
        int [] arr2=new int[string1.countTokens()];
        n=arr2;

        //assigns every token to an array element for better application
        i=-1;
        do
        {
            i++;
            tokens[i]=string1.nextToken();
            System.out.println(tokens[i]);

        }while(string1.hasMoreTokens());

        System.out.println();

        //checks if the entered words corresponds to a number
        k=-1;//if one thousand one
        j=0;
        do
        {
            k++;
            do
            {
                if((j!=tokens.length)&&(k!=num_words.length))
                {
                    if(tokens[j].equalsIgnoreCase(num_words[k]))
                    {
                        first=num[k];
                        j++;
                        send(num[k],j);
                        k=-1;
                        break;
                    }
                    else
                    {
                        if(tokens[j].equalsIgnoreCase("and"))
                        {

                            point(tokens);
                            break;
                        }
                        else
                        {
                            j=choice(tokens[j],j);
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }while(k<num_words.length);

        }while(k<num_words.length);

        if(first==-1)
        {
            JOptionPane.showMessageDialog(null,"Sorry you did not enter a numered word!!!");
            System.exit(0);
        }
        method1();
        System.out.print("First: \n"+first+"\nLength: \n"+num_words.length);
    }

}


this is the whole code that we made, we would really appreciate some comments, help or guidance. We are currently just beginners in programming and mostly rely on books only, because our professor hasn't taught us that much yet. thanks!
User is offlineProfile CardPM
+Quote Post

Programmist
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 06:20 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
First, I'd put my constants in an enum class and give them numerical values. If you don't know how to use enum, Google Java enum tutorial. Second, you have to think about how spoken number work.

990,100.50 is
nine hundred and ninety thousand one hundred point fifty.

So let's break down what this means:

nine hundred = nine times a hundred
nine hundred and ninety = nine times a hundred plus ninety
nine hundred and ninety thousand = (nine times a hundred plus ninety) times a thousand

one hundred and fifty = one hundred plus fifty

nine hundred ninety thousand one hundred and fifty = ((nine times a hundred plus ninety) times a thousand) plus one hundred plus fifty

See the pattern? So, you can add constants for 100, and 1000. If you're expecting higher numbers add constants for other powers of 10. These are your multipliers.

HTH

This post has been edited by alcdotcom: 29 Jun, 2007 - 06:22 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 06:25 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,124



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
This is how I change char to int, maybe this can help?

CODE

String strday = request.getParameter("day");
int day=Integer.Parseint(strday);

User is online!Profile CardPM
+Quote Post

Programmist
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 08:01 AM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(no2pencil @ 29 Jun, 2007 - 07:25 AM) *

This is how I change char to int, maybe this can help?

CODE

String strday = request.getParameter("day");
int day=Integer.Parseint(strday);



No - they are not looking for a way to change a string representation of an integer to a int. They are looking for a way to change a spoken numeral into an Number (in or float?).

e.g.
"42" to 42 - NO
forty two to 42 - YES
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 08:09 AM
Post #5

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
there will be a ridiculously large number of cases to consider. Especially if you are going to use incorrect terminology for decimals... all digits after the decimal are meant to be single, eg 0.50 is decimal five zero, not decimal fifty. The reasoning is simple if i gave you: 0.123456789347583473243759235735832732 say it your way...

is it meant to take numbered input "one" or is it to modify a message which may contain these words? As the latter is more useful, but a little more work.

you will also have to take into account numbers which have "-" ex: twenty-four.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 10:11 AM
Post #6

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(William_Wilson @ 29 Jun, 2007 - 09:09 AM) *

there will be a ridiculously large number of cases to consider. Especially if you are going to use incorrect terminology for decimals... all digits after the decimal are meant to be single, eg 0.50 is decimal five zero, not decimal fifty. The reasoning is simple if i gave you: 0.123456789347583473243759235735832732 say it your way...

is it meant to take numbered input "one" or is it to modify a message which may contain these words? As the latter is more useful, but a little more work.

you will also have to take into account numbers which have "-" ex: twenty-four.


Agreed. I was going under the assumption that this was a homework assignment and therefore had some limits on the inputs to moderate the complexity. I get so used to dealing with homework questions that I forget that there are non-students here. smile.gif
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How Can To Change An Input Of Number In Words To Integers..
29 Jun, 2007 - 10:18 AM
Post #7

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
fair enough, i'm sure 80% of the coding questions start from or are directly related to school work smile.gif
User is offlineProfile CardPM
+Quote Post

Pulse
RE: How Can To Change An Input Of Number In Words To Integers..
30 Jun, 2007 - 06:25 AM
Post #8

New D.I.C Head
*

Joined: 28 Jun, 2007
Posts: 3


My Contributions
@alcdotcom
thanks for the info about the enum class. I've discussed it with my groupmates and we all agreed to use it. we are currently and slowly studying the things we can do with that class.
I've asked our prof if we could change the format of how the words should be typed because i preferred the one you stated in your previous post but he disagreed and said that we should stick to the format he explained anime2.gif(the one that i previously posted)

@William_Wilson
oh i forgot to mention that the decimals places are only limited to two "0.00".hehe. sorry sorry.


we are currently creating a new set of codes because we thought our previous code was too complicated. anyway, i'll post it again here if we're done with our new code. we'll be really gratefull for any opinion, comments, suggestions and tips that you can give us.
User is offlineProfile CardPM
+Quote Post

retrac241
RE: How Can To Change An Input Of Number In Words To Integers..
6 Jul, 2007 - 03:09 AM
Post #9

New D.I.C Head
*

Joined: 6 Jul, 2007
Posts: 1


My Contributions
Hooooaaaaa!!! you've got a really long code for that program... crazy.gif

We also had an assignment on making a java program which converts a number in words to roman numerals...
what is the range of your input value?....
ours is from 0.00 to 999,999,999,999.99 cool.gif

I will help you with the conversion but not with the validation (if there is any validation required)...
pirate.gif
User is offlineProfile CardPM
+Quote Post

Pulse
RE: How Can To Change An Input Of Number In Words To Integers..
6 Jul, 2007 - 02:36 PM
Post #10

New D.I.C Head
*

Joined: 28 Jun, 2007
Posts: 3


My Contributions
we have the same range of values. and yup, i need validation but don't worry i don't intend to get direct codes from the people here. i only need tips. later today i will post our new and hopefully finished code. i'll try to make the program short this time tongue.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:41PM