(Java) Print given integer in written character?

I have this assignment where I'm supposed to write a program that takes an integer from a scanner up to four digits and prints it in written form.

Output example:

0       : zero

13     : thirteen

101   : one hundred and one

7903 : seven thousand nine hundred and 3

I've been looking at char and string variables and I'm guessing maybe the clue is char and UNICODE. Ideas?

 

You would need to use a switch and include every single number.

 

Do they really want numbers like "thirteen" or do they want "one three" for the output of 13? Cause if they just want "one three" you could just use a for loop and a switch that goes through each number and evaluates it.

Yeah, like eleven, twelve, thirteen.. etc etc

Also forgot to mention that we're only allowed to use a few selections of statements and classes like for/while/do-while loops, all data types(char, boolean, int etc) if/else, Math/Random classes, Scanner, operands/operators. method calling, helper methods

No arrays, GUI or recursions. Switches I don't think is even specified in my text book (which is "Building Java Programs" - Stuart Reges, Marty Stepp)

Ok that is a little bit more difficult but here is a link to a very similar problem.

http://stackoverflow.com/questions/12284768/write-code-to-convert-given-number-into-words-eg-1234-as-input-should-output-on

Can you use Lists and Maps?  Not being able to use arrays is fine, as long as, you can use Lists and Maps.

And not being able to use recursion is not a problem either; anything can do with recursion you can do using loops.  It is just your code will not be as concise.  

The link provided by trueBearsFan lists code that is a solution to your project, but it uses both recursion and arrays.  So translating that to Java will not be the answer.

Since the project specifies that you convert integers that means you only need to worry about numbers up to 2,147,483,647.  Did your professor specify that you also have to convert negative integers?  It does not really matter, the conversion will be the same; you just have to prefix “negative” to your answer if the integer is less than zero.  Just make sure you account for it.

Have you covered enums yet?  If you can use enums, you can actually map the conversion logic to a set of enum values that represent the order of magnitude place names:  TEN, HUNDRED, THOUSOUND, MILLION, and BILLION.  Then you can break your inputted integer value into its respective place values, and pass each value to the respective enum to perform the conversion.  I would use a String—or better yet a StringBuilder--to break up the integer value into its place values.  It is much easier to manipulate the inputted value as a String (or StringBuilder) than as an integer.

This is pretty much the same method you used as a kid, when you were taught how to read numeric values as English words.

 

He said that it is a four digit scanned in number so only up to 9999.

Oh, I guess I missed that part.  That makes the problem even easier, but once you have solved the problem up to the thousands place, solving it for all possible integer values is pretty easy and straightforward.

I may look into the StringBuilder class, but we also got word from the professor that we're now allowed to use anything, so would arrays be another solution?

Edit: I've decided to give string arrays a shot. Here's what I've done so far:

The method "printOnes" works fine as it passes an integer value between 0 and 19 into the array index and returns the correct string. I am however a bit stumped on how I'm supposed to "glue together" the strings that correspond to larger integers like 21, 89 and 6345

Code is available here: http://pastebin.com/FNytQrtG

What do you mean by you are not allowed to use anything?  Does that mean you cannot use any JDK classes for your solution, e.g., HashMap, ArrayList, etc? 

And you said arrays and recursion were off limits, is that not the case?

If you cannot use any JDK classes for your solution, but can use arrays and recursion then you can take the solution provided by trueBearsFan and translate it to Java.  Just make sure you understand the code.

If you cannot use recursion, you can take the solution provided by trueBearsFan, translate it to Java and convert it to use loops instead of recursion.  This will probably be your easiest solution.

If you are allowed to use Java enums, you can also use the solution I suggested earlier.  Java enums are specialized Java classes that lend themselves well to defining and processing finite, known data sets.

If you are still having problems here’s a partial solution to your project.  I took your code and made a couple of changes, restructured your conversion logic, and give an example of how to concatenate the results.  I did not use your scanner logic, I just created a very simple test driver.  You will still need to expand this to convert the thousands place from a numeric value to its word form and piece everything together, but this is very straightforward.  Just make sure you understand the code, and if you have questions, let me know.

I did this in about 20 minutes, so it is a simple quick solution, and you will probably have to add error checking.  Just make sure you test your project with lots of different values including ones that should fail and make sure your project accounts for this.  This is a habit you should develop early, so you learn to write solid code.

I moved all of the conversion logic into anonymous inner classes.  If you are unfamiliar or uncomfortable with anonymous inner classes or your professor will not let you use them, you can convert these into regular Java classes that implement the ConvertNumbersToWords interface.  

If your professor will not let you use separate classes for your project, you can just convert all of the anonymous inner class’ convertNumber methods to methods within a single class.  Just give the methods different names, e.g., convertHundredsNumber.

Pastebin location of the code:

http://pastebin.com/3u75AB8W