Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Need to convert an integer input to a text output

  Asked By: Lisa    Date: Apr 26    Category: Java    Views: 1850

I've just started a course in Java, and am enjoying it even though
it can cause me to tear my hair out at times!

My problem is this: I've been (attempting!) creating a program
that will read an integer input (between 0-99), and convert them
to a text output; for example, if I were to input 99, the output will
be ninety nine. Basically, I have no idea how to convert integers
into a text output! I suspect it's to do with the the ConsoleWindow
class and/or the Printstream class, but I've no idea what to type
in. Considering that I've only just started to use if statements
should give you an idea of how much of a newbie I am at this!
Books I've read refer to something called Integerparse, but I've
asked friends about this and apparently this won't be covered for
at least another month, so the solution would (apparently!) be at
a more basic level!



6 Answers Found

Answer #1    Answered By: Corinne Rogers     Answered On: Apr 26

To do this,
You should if statements or switch statement

You should take the first number of input  and insert it to switch
then you should the second number of the input and insert it into switch

Answer #2    Answered By: Agatha Miller     Answered On: Apr 26

I'm unsure as to how to do this. Obviously, I'm looking at this
problem from the wrong angle. Switch statements have only
been covered very briefly...we've not even reached while and for
yet! I suspect maybe I'm attempting to answer the question in a
too sophisticated manner, in that I'm looking for the java  program
to do the work for me in converting numeric inputs into a textual

Answer #3    Answered By: Sonya Flores     Answered On: Apr 26

something like this maybe..........

System.out.println("Pleaser enter two numbers");
int num = 0;
char ch = System.in.read();
char ch2 = System.in.read();
if(ch>='0' && ch<='9' && ch2>='0' && ch2<='9')
num = (ch-'0') * 10 + ch2 - '0';
else System.out.println("I said a number you fool");

Answer #4    Answered By: Eric Foster     Answered On: Apr 26

Except if you are learning programming for the first time, put {}
brackets around your if block and again around your else block.

You don't need to, but it is a pain in the bum trying to find these kind
of errors in code.

Answer #5    Answered By: Oliver Evans     Answered On: Apr 26

Are you working on console or GUI?

Answer #6    Answered By: Geb Chalthoum     Answered 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

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.

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.