# Convert Numbers from -999 to 999 in words!

• May 19th, 2014, 03:13 PM
loma
Convert Numbers from -999 to 999 in words!
Hi, I need help with my program written in DrJava. I'm very new to Java and need to write a program where the user inputs any number from -999 to 999 and it inputs it in words. For ex -24 entered would print negative twenty four or 800 entered would be eight hundred. I need to have at least 2 methods that need to be returned to the main method. I have some ideas so far but need help. When i compile, it says that cannot convert from void to java.lang.string for all the word = ... in my case statements. Help me! Please and thank you very much. I appreciate you thoughts and advice!

Code Java:

```import java.util.Scanner;   public class NumToText { public static void main (String args[]){   Scanner input = new Scanner (System.in) ; System.out.println ("Enter number."); int number = input.nextInt ();     if (number > 99 && number < 1000) { int h = number / 100; //find the hundreds only, next step to print tens and ones hundreds(h); //print number   // test if they are from 11 to 19 called teens   int x = 0; // initialized variable x for calculations x = number % 100; // find remainder of hundreds like 51   if (x > 10 && x < 20) { // ex,is 51 a teens number? teens (x); // print number } // if not teens number -> split up into tens & units if (x > 0 && x < 100) {   int tens = x / 10; // find the tens tens(tens); // print number   int ones = x % 10; // finding the units (one, two) ones(ones); // print number } }     }   static String ones (int number) {   String word = "";   switch(number) { case 0: word= System.out.print("zero"); break; case 1: word= System.out.print("one"); break; case 2: word= System.out.print("two"); break; case 3: word= System.out.print("three"); break; case 4: word= System.out.print("four"); break; case 5: word= System.out.print("five"); break; case 6: word= System.out.print("six"); break; case 7: word= System.out.print("seven"); break; case 8: word= System.out.print("eight"); break; case 9: word= System.out.print("nine"); break; } return word; }   static String teens (int number) {   String word = "";   switch(number) { case 11: word= System.out.print("eleven"); break; case 12: word= System.out.print("twelve"); break; case 13: word= System.out.print("thirteen"); break; case 14: word= System.out.print("fourteen"); break; case 15: word= System.out.print("fifteen"); break; case 16: word= System.out.print("sixteen"); break; case 17: word= System.out.print("seventeen"); break; case 18: word= System.out.print("eighteen"); break; case 19: word= System.out.print("nineteen"); break;   } return word; }   static String tens (int number) {   String word = "";   switch(number) { case 10: word= System.out.print("ten"); break; case 20: word= System.out.print("twenty"); break; case 30: word= System.out.print("thirty"); break; case 40: word= System.out.print("fourty"); break; case 50: word= System.out.print("fifty"); break; case 60: word= System.out.print("sixty"); break; case 70: word= System.out.print("seventy"); break; case 80: word= System.out.print("eighty"); break; case 90: word= System.out.print("ninety"); break;   } return word; }   static String hundreds (int number) {   String word = "";   switch(number) { case 10: word= System.out.print("one hundred"); break; case 2: word= System.out.print("two hundred"); break; case 3: word= System.out.print("three hundred"); break; case 4: word= System.out.print("four hundred"); break; case 5: word= System.out.print("five hundred"); break; case 6: word= System.out.print("six hundred"); break; case 7: word= System.out.print("seven hundred"); break; case 8: word= System.out.print("eight hundred"); break; case 9: word= System.out.print("nine hundred"); break;   } return word; } }```
• May 19th, 2014, 03:48 PM
GregBrannon
Re: Convert Numbers from -999 to 999 in words!
Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

You've identified the problems. These statements are not valid Java statements:

word = System.out.print("one");

Assigning an output statement to a String variable is . . . well, nonsense to the compiler and confusing to humans wondering what you're trying to do.

In the above statement, I think you're trying to assign "one" to word, which would simply be:

word = "one";

Try converting your existing statements to the more simple format I've given you and then come back when you have more questions.
• May 19th, 2014, 08:20 PM
dicdic
Re: Convert Numbers from -999 to 999 in words!
Is it necessary to use a switch statement? Actually you can use another algorithm which is better than that and an easy way of converting number figures to words. Ex:

Assign an array of String whose values are one, two... nine.
Ex:
String[] ones = {"","one","two","three","four","five","six","seven ","eight","nine"};

As you noticed, value of first element in array is empty, well the reason is we are not going to use that, we are going to use the indexes of element as the number figure equivalent of each number word.
word "one" is at index 1
word "two" is at index 2.

so if I input 1 I'll just print it's value in String Array.
Code java:

```public static void main(String[] args) { String[] ones = {"", "one", "two","three", "four", "five", "six", "seven", "eight", "nine"}; int sampleInput = 2; System.out.println(ones[sampleInput]); }```
example code above will print "two" as element of ones array at index 2 has a value of "two"

but what about the numbers compose of multiple digits? just create another array of each unit
Ex:
String[] ones = {"","one","two","three","four","five","six","seven ","eight","nine"};
String[] tens = {"", "", "twenty", "thirty","forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

as you notice, there is not "ten" in tens array, It's because I believed that "ten" belongs to special numbers like 11,12-19

so how are you going to print 24?
here it is:
just get the first digit which is 2
then print the element in array tens using index 2:
tens[2] = twenty
then print space
then get the one's digit and print the element in array ones:
ones[4] = four.

that would make an output of twenty four

just create another array let say units
String[] units = {"", "thousand", "million", "billion"}; // you can expand it even to fillions
I did not put "hundred" in it. (I don't know how to explain, I just don't feel to put hundred)

how are we going to print 891?
here it is
get the first digit 8
then print ones[8] = eight
then print space
then print "hundred" print space
next get the second digit 9
then print tens[9] = ninety
then print space
then get the last digit 1
then print ones[1]
that will make an output of eight hundred ninety one

and actually there is a pattern in converting number figures to words:
999 999 999 999 is equal to:
nine hundred ninety nine billion
nine hundred ninety nine million
nine hundred ninety nine thousand
nine hundred ninety nine

you just have to be creative in coding.
• May 20th, 2014, 07:38 PM
loma
Re: Convert Numbers from -999 to 999 in words!
Quote:

Originally Posted by GregBrannon
Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

You've identified the problems.

Okay. I have figured it out somewhat. I have got it to print from 0 to 999 by taking your advice. Now i'm just wondering how to make it do the same program but with negative numbers. Below is the updated part of my code where its in the main method and is right below the Scanner code lines and before my switch cases. Nothing else has changed. When i add the if (number < 0) part, it shows negative in the output but doesnt show the rest of the number like 123. It just says negative. And not the 123 because it doesn't check the other ifs because i said like for example > 99 and < 1000 so it doesn't check the negatives. Thank you again.
Code Java:

```if (number == 0){ System.out.print ("zero"); }   if (number < 0){ System.out.print ("negative" ); }     if (number > 99 && number < 1000) { int h = number / 100; //find the hundreds only, next step to print tens and ones System.out.print (hundreds(h)+ " and "); }   int x = 0; // initialized variable x for calculations x = number % 100;     if (x > 0 && x < 100) {   if (x > 10 && x < 20) { System.out.print (teens (x)); } else {   int tens = x / 10; // find the tens System.out.print(tens(tens)+ " ");   int ones = x % 10; // finding the ones System.out.print (ones(ones)); } } }```
• May 20th, 2014, 09:04 PM
dicdic
Re: Convert Numbers from -999 to 999 in words!
can you paste your whole code?

--- Update ---

Ok, I think I figured it out.
see this conditions in your code:
if (number > 99 && number < 1000)
if (x > 0 && x < 100)

those conditions above will never satisfy if the number is less than zero meaning negative.

now look at this statement in your code:
if (number < 0){
System.out.print ("negative" );
}

you better convert number to positive value or its absolute value when you satisfy that condition.
how? just multiply it by -1.
• May 25th, 2014, 12:41 PM
loma
Re: Convert Numbers from -999 to 999 in words!
Quote:

Originally Posted by dicdic
can you paste your whole code?

--- Update ---

Ok, I think I figured it out.
see this conditions in your code:
if (number > 99 && number < 1000)
if (x > 0 && x < 100)

those conditions above will never satisfy if the number is less than zero meaning negative.

now look at this statement in your code:
if (number < 0){
System.out.print ("negative" );
}

you better convert number to positive value or its absolute value when you satisfy that condition.
how? just multiply it by -1.

i just solved it an hour after i posted my reply by multiplying it by negative one. :))

thank you!