Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lisa Anderson   on Apr 26 In Java Category.

  
Question Answered By: Geb Chalthoum    on Apr 26

Java won't help you (directly) to create text  from a number. You need to do a
couple of things ...

First is to separate your number into its tens and units portions. Tens is
easy, simply divide by 10 and take the int result. Units requires the modulo
division operator - which is the percent sign.

int units = myNumber % 10;

Now you need to look up the words you want. There are (at least) three ways to
do this:

You can look it up using a switch statement
You can look it up using nested "if ... else if ... else ..."
You can have the words in a couple of arrays and look them up by indexing the
arrays.

Use the one that is closest to what you're doing in class.

You will presumably want to get the output  approximately right (e.g. no word for
zero units unless tens are also zero; no word for zero tens at all; 11 to 19
converted correctly). The zero tens can be done by converting it to an empty
string, the others will need their own "if" statements (with a separate switch,
if nest or array for the teens).

Hope this is enough information to get your research started. Once you've had a
good go, if you are still having problems, post the code and we'll be glad to
provide pointers.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Need to convert an integer input to a text output Or get search suggestion and latest updates.


Tagged: