-1

I want to create a program that would allow a user to input a number in an EditText and display its English word representation. What is the fastest way to create this program with input from 1-1000?

Example:

Input: 1
Output: One

The code i used :

public class MainActivity extends Activity { 

Button submit;
EditText ET;
TextView output;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

submit = (Button) findViewById(R.id.SubmitBTN);
ET = (EditText) findViewById(R.id.editText1);
output = (TextView) findViewById(R.id.textView1);

Submit.setOnClickListener(new OnClickListener(){

int num = Integer.parseInt(ET.getText().toString());

Switch(num){

case: 1
output.setText("One");
break;

case: 2
output.setText("Two");
break;

case: 3
output.setText("Three");
break;

case: 4
output.setText("Four");
break;

case: 5
output.setText("Five");
break;

}

         });
     }
}
YFeizi
  • 1,498
  • 3
  • 28
  • 50
Jarobe Bernardo
  • 75
  • 1
  • 10
  • 1
    http://stackoverflow.com/questions/21521520/how-to-convert-an-int-so-as-to-return-the-string-which-is-how-we-say-the-int – Neverwork2123 Feb 10 '15 at 04:35

3 Answers3

0

Neither Java or Android API does not contain any support for spelling numeric values though you can implement your own algorithm or try these answer's for examples.

Community
  • 1
  • 1
0

you can use this class and call it from your main

NumberToWord convertToWord = NumberToWord()

convertToWord.convert(num); //num <- your variable that holds the input

this will convert from one to quintillion.

public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " fourty",
        " 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 String convertLessThanOneThousand(int number) {
        String current;

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

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

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

}

credit: http://javahungry.blogspot.com/2014/05/convert-math-number-to-equivalent-readable-word-in-java-code-with-example.html

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
-1
            public class MainActivity extends Activity { 

            Button submit;
            EditText ET;
            TextView output;
            String words[]={"one","two","three","four","five"};

            @override 
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            submit = (Button) findViewById(R.id.SubmitBTN);
            ET = (EditText) findViewById(R.id.editText1);
            output = (TextView) findViewById(R.id.textView1);

            Submit.setOnClickListener(new OnClickListener(){ 

            int num = Integer.parseInt(ET.getText().toString());
            if(num<5)
            output.setText(words[num+1]); 


                     }); 
                 } 
            } 
Sonu Raj
  • 369
  • 3
  • 11
  • 1
    i change the code output.setText(words[num+1]); to output.setText(words[num-1]); and it works – Jarobe Bernardo Feb 10 '15 at 07:13
  • This code will produce erroneous results (off by 2) and an [ArrayIndexOutOfBounds](http://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException.html) very soon. – Phantômaxx Feb 10 '15 at 07:36
  • this is just a sample and I have not handled it. To handle it just put some condition – Sonu Raj Feb 10 '15 at 08:35