Using StringTokenizer in Coverting words to number

孤街醉人 提交于 2019-12-22 19:23:08

问题


Can anyone please help me on how to convert words into numbers in Java programming, using string tokenizer. Your answer will be highly appreciated.

EDITED : I already made this code . but there is an error . like when i input one thousand one hundred , the program outputs 100100 . need your help guys . what do you think is the problem with my program and also what should i do . thanks alot ..

    import javax.swing.*;
         import java.util.*;
         import java.text.*;
         public class convertwordstonumbers {
         public static void main(String[] args) {

         String sInput;
         sInput=JOptionPane.showInputDialog("Enter a word/s:");
         StringTokenizer sToken= new StringTokenizer(sInput);
         int Tokens=sToken.countTokens();
         String Words[]=new String[Tokens];
         double Numbers[]=new double[Tokens];
         double Multiplier[]=new double[Tokens];
         String Place[]=new String[Tokens];
         int a=0;
         while(sToken.hasMoreTokens())
         {
        Words[a]=sToken.nextToken();
        a++;
        }
        String sUnits[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
        String sTeens[]=   {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        String   sTys[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        String  sIons[]={"hundred","thousand","million","billion"};
        String   sThs[]={"tenths","hundredths"};
        double iUnits[]={0,1,2,3,4,5,6,7,8,9};
        double iTeens[]={10,11,12,13,14,15,16,17,18,19};
        double   iTys[]={20,30,40,50,60,70,80,90};
        double  iIons[]={100,1000,1000000,1000000000};
        double  iDecs[]={0.1,0.01};
        double iSum=0;
        for(int b=0;b<Tokens;b++){
        for(int c=0;c<10;c++){
            if(Words[b].compareToIgnoreCase(sUnits[c])==0){
                Numbers[b]=iUnits[c];
                Place[b]="a";
            }
        }
        for(int c=0;c<10;c++){
            if(Words[b].compareToIgnoreCase(sTeens[c])==0){
                Numbers[b]=iTeens[c];
                Place[b]="a";
            }
        }
        for(int c=0;c<8;c++){
            if(Words[b].compareToIgnoreCase(sTys[c])==0){
                Numbers[b]=iTys[c];
                Place[b]="a";
            }
        }
        for(int c=0;c<4;c++){
            if(Words[b].compareToIgnoreCase(sIons[c])==0){
                Numbers[b]=iIons[c];
                Place[b]="b";
                Multiplier[b]=iIons[c];
            }
        }
        for(int c=0;c<2;c++){
            if(Words[b].compareToIgnoreCase(sThs[c])==0){
                Numbers[b]=iDecs[c];
                Place[b]="b";
            }
        }
    }
        for(int d=0;d<Tokens;d++){
        if(Place[d]==null){
            JOptionPane.showMessageDialog(null, "Invalid input");
            System.exit(0);
        }
        if(Place[d]=="a")
            iSum+=Numbers[d];
        if(Place[d]=="b")
            iSum*=Numbers[d];
    }
   if (iSum<1000)
    {DecimalFormat dFormat= new DecimalFormat("0.00");
    System.out.println(dFormat.format(iSum));}
         else 
         {DecimalFormat dFormat= new DecimalFormat("0,000.00");
         System.out.println(dFormat.format(iSum));
            }
    }``
}

回答1:


I think stringtokenizer is not enough, you need a dictionary to transalate from your language to number

see also http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ie/NumberNormalizer.html

Provides functions for converting words to numbers Unlike QuantifiableEntityNormalizer that normalizes various types of quantifiable entities like money and dates, NumberNormalizer only normalizes numeric expressions (e.g. one => 1, two hundred => 200.0 )




回答2:


The StringTokenizer has a constructor that allows specific delimiters. Assuming you wish to delimit each token by a mere space " " you could use a ArrayList with each token pre-specifically placed using numbers 0-9.

 ArrayList<String> token;

 token.add("zero");
 token.add("one"); ... etc

 StringTokenizer s = new StringTokenizer("four zero four", " ");
 String num_rep = token.indexOf(s.nextToken()) + token.indexOf(s.nextToken()) + token.indexOf(s.nextToken());



回答3:


You need to write a logic for comparing the string with standard words, and then building the number on the go.

Just an observation: StackOverflow is not a place to get your assignments solved.



来源:https://stackoverflow.com/questions/21145754/using-stringtokenizer-in-coverting-words-to-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!