【问题标题】:Using StringTokenizer in Coverting words to number在将单词转换为数字时使用 StringTokenizer
【发布时间】:2014-01-15 18:46:34
【问题描述】:

任何人都可以帮助我如何在 Java 编程中使用字符串标记器将单词转换为数字。您的回答将不胜感激。

编辑: 我已经制作了这段代码。但有一个错误。就像我输入一千一百,程序输出 100100 。需要你们的帮助。你认为我的程序有什么问题,我应该怎么做。非常感谢..

    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));
            }
    }``
}

【问题讨论】:

  • 这是作业题吗?

标签: java stringtokenizer


【解决方案1】:

我认为 stringtokenizer 还不够,你需要一个字典来从你的语言翻译成数字

另见 http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ie/NumberNormalizer.html

提供将单词转换为数字的函数与 QuantifiableEntityNormalizer 对各种类型的可量化实体(如货币和日期)进行规范化不同,NumberNormalizer 仅对数字表达式进行规范化(例如,一 => 1、两百 => 200.0)

【讨论】:

  • 我编辑了我的问题先生,感谢您的回答。它真的对我有帮助。非常感谢..
【解决方案2】:

StringTokenizer 有一个允许使用特定分隔符的构造函数。假设您希望仅用空格“”分隔每个标记,则可以使用 ArrayList,其中每个标记预先使用数字 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】:

您需要编写一个逻辑,将字符串与标准单词进行比较,然后随时构建数字。

只是一个观察:StackOverflow 不是解决任务的地方。

【讨论】:

  • 我编辑了我的问题先生,感谢您的回答。它真的对我有帮助。非常感谢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多