ANDROID:转换输入的号码单词 [英] ANDROID: Converting inputted numbers to words

查看:138
本文介绍了ANDROID:转换输入的号码单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的分配状态:123 = 123。我是CLUE​​LESS!后来我发现这个网站 http://www.rgagnon.com/javadetails/java-0426。 HTML
但我真的不知道如何将其转化成机器人。

Our assignment states that we write a program that will input a number up to 6 digits and will convert the numbers into words.. ex: 123 = one hundred twenty three. I AM CLUELESS! Then I found this site http://www.rgagnon.com/javadetails/java-0426.html but I don't really understand how to convert it into android.

请帮忙me..please。

please help me..please.

这是我的Main_Activity.xml:

here is my Main_Activity.xml:

package com.example.torres;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    final EditText numbers =  (EditText) findViewById(R.id.editText1);
    final EditText results = (EditText) findViewById(R.id.editText2);

    Button btnConvert = (Button) findViewById(R.id.button1);     
    btnConvert.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
         String numberz =numbers.getText().toString();
         try {
             final long number = Long.parseLong(numberz);
             String returnz = Words.convert(number); 
         } catch ( NumberFormatException e) {
             //Toast.makeToast("illegal number or empty number" , toast.long)
         }

                       }  });


        }}

和这里是我的Words.java

and here is my Words.java

package com.example.torres;

import java.text.DecimalFormat;

public class Words  {




      private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
      };

      private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
      };

      private Words() {}


    public static String convertLessThanOneThousand(int number) {
        String soFar;

        if (number % 100 < 20){
          soFar = numNames[number % 100];
          number /= 100;
        }
        else {
          soFar = numNames[number % 10];
          number /= 10;

          soFar = tensNames[number % 10] + soFar;
          number /= 10;
        }
        if (number == 0) return soFar;
        return numNames[number] + " hundred" + soFar;
      }


      public static String convert(long number) {
        // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }

        String snumber = Long.toString(number);

        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);

        // XXXnnnnnnnnn
        int billions = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int millions  = Integer.parseInt(snumber.substring(3,6));
        // nnnnnnXXXnnn
        int hundredThousands = Integer.parseInt(snumber.substring(6,9));
        // nnnnnnnnnXXX
        int thousands = Integer.parseInt(snumber.substring(9,12));

        String tradBillions;
        switch (billions) {
        case 0:
          tradBillions = "";
          break;
        case 1 :
          tradBillions = convertLessThanOneThousand(billions)
          + " billion ";
          break;
        default :
          tradBillions = convertLessThanOneThousand(billions)
          + " billion ";
        }
        String result =  tradBillions;

        String tradMillions;
        switch (millions) {
        case 0:
          tradMillions = "";
          break;
        case 1 :
          tradMillions = convertLessThanOneThousand(millions)
             + " million ";
          break;
        default :
          tradMillions = convertLessThanOneThousand(millions)
             + " million ";
        }
        result =  result + tradMillions;

        String tradHundredThousands;
        switch (hundredThousands) {
        case 0:
          tradHundredThousands = "";
          break;
        case 1 :
          tradHundredThousands = "one thousand ";
          break;
        default :
          tradHundredThousands = convertLessThanOneThousand(hundredThousands)
             + " thousand ";
        }
        result =  result + tradHundredThousands;

        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result =  result + tradThousand;

        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");}




      }

唯一的问题是现在不会发生什么意外笑

The only problem now is that nothing happens lol

推荐答案

您可以单独类的 EnglishNumberToWords 如示例中的链接。

You can make separate class for EnglishNumberToWords as in the example link.

和在你点击链接,你必须只需拨打

and in your button click you have to just call

String return_val_in_english =   EnglishNumberToWords.convert(YOUR_NUMBER_TO_CONVERT);

这篇关于ANDROID:转换输入的号码单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆