-1

I've just started doing some java, and I'm not really sure how to approach this task.

Write a program that receives a number (1-4 digits) from the user, then prints the number as text.

For example: 
Input: 1234
output: one thousand two hundred and thirty-four.

Lecturer recommended using arrays, because that makes the code a lot simpler. Can someone point me in the right direction.

I know how to do the first part, using the scanner to receive the number. I'm just not sure what's the simplest way to "translate" it into text without using a hundred lines of code.

Thanks in advance.

Nabin
  • 11,216
  • 8
  • 63
  • 98
ScypeR
  • 1

2 Answers2

2

You can use a switch-case!

  1. Read the Number
  2. Split every Number into a Array!
  3. Than you can check how many inputs in the array -> This makes the decision whats the biggest word is ( for example: Billion, Million, Thousand ....)
  4. You have to add a for-repeater that do step by step every task
  5. In the for-repeater you have to check the input of the array -> Than switch it from number to word ( for example: if in the array is a 4, you have to check it with your switch and than print a four!)
Lukas Hieronimus Adler
  • 1,063
  • 1
  • 17
  • 44
  • Using [Integer.ParseInt(String s)](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)) gets you the value faster than splitting it. – Dawnkeeper Oct 23 '14 at 11:41
0
  1. Write a program to deal with just one digit numbers. Test and correct your program.

  2. Extend your program from 1. to deal with two digit numbers as well. Test and correct.

  3. Further extend your program to deal with three and four digit numbers. Test and correct.

  4. Look for ways to reduce duplication of code, possibly by using arrays and loops. Test thoroughly and correct.

It is often easier to start with a solution to a simpler problem and work up to the final more complex problem and its solution.

rossum
  • 15,344
  • 1
  • 24
  • 38