# Number to words in left to right direction

I have faced this question in an interview:

If we enter a number it will convert into individual strings and display in same order.

Ex: If I enter 564 then the output should be FIVE SIX FOUR.

I have written the following working code (of course it has some limitations). Suggest any possible ways of optimizing this code.

public class NoToWord {

public static void main(String[] a)
{
int no = 0;
String[] words= new String[]{"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
String[] word=new String[10];

try {
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i,j;
j=0;
while(no>0){
i = no%10;
no /= 10;
word[j] = words[i];
j++;
}
for(int k= word.length;k>0;k--)
{
System.out.println(word[k-1]+" ");
}
}
}

• what about -1? how would you handle it? – Dejel Apr 4 '16 at 18:41

This code:

int i,j;
j=0;
while(no>0){
i = no%10;
no /= 10;
word[j] = words[i];
j++;
}
for(int k= word.length;k>0;k--)
{
System.out.println(word[k-1]+" ");
}


can be drastically simplified to:

String numbers = String.valueOf(no);
for(int i = 0 ;i<numbers.length(); i++){
System.out.print(words[Character.getNumericValue(numbers.charAt(i))]+" ");
}


Or you may save save the user input as String (but make sure it only contains digits). You will have something like this:

 String numbers = br.readLine();
for(int i = 0 ;i<numbers.length(); i++){
System.out.print(words[Character.getNumericValue(numbers.charAt(i))]+" ");
}

• this code is perfect and working fine(I tried). But is there any common code(not in java like algorithm) for all languages? – user3667402 May 23 '14 at 4:32
• You mean if there's a way that doesn't rely/heavily rely on Java methods... (not algos)? – h.j.k. May 23 '14 at 5:47

Besides the points mentioned by @DnR, I'm not sure why you choose to create a 10-element resultant String array. You can just use System.out.print() in your while loop to print the word out, remembering to use StringBuilder to reverse() the input String first.

Also, if you want to stay away from Character.getNumericValue() you can try these:

int zero = '0' - 48; // will be 0
int one = '1' - '0'; // similar


A character value minus the character '0' or the number 48 gives you the number to pass to your String array (because '0' has an integer value of 48 in ASCII).

I'll also advocate number over no at the very least... it's easier to read.

• You should actually just subtract '0' out. :) Like: int val = myString[i] - '0'; – fluffy Nov 27 '15 at 18:42
• @fluffy that's a good point. :) – h.j.k. Nov 28 '15 at 3:15

Well, this should be much more eficient, by only using one loop:

public class NoToWord {

public static void main(String[] a){
int no = 0;
String[] words= new String[]{"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
String[] word=new String[10];

try {
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

String numbers = String.valueOf(no);

for(int i = 0 ;i<numbers.length(); i++){
System.out.print(words[Character.getNumericValue(numbers.charAt(i))]+" ");
}
}
}

• this code is perfect and working fine(I tried). But is there any common code(not in java like algorithm) for all languages? – user3667402 May 23 '14 at 4:35

I think it is good to validate your input. You do it by parsing it to an Integer. You could also make a method that uses Character.isDigit() for all characters in your String. That way you can act on arbitraty length input.

If the input is valid, you can simply loop over the characters in the input and convert them. While you can use the 'trick' from h.j.k.'s answer, I prefer using explicit methods to convert digits to int.

You can even combine these methods

for (char c : input.toCharArray())
{
if (!Character.isDigit(c))
{
throw new IllegalArgumentException("'input' contains invalid characters");
}
int index = Character.digit(c, 10);
...
}