# How to convert a digit to words

• March 5th, 2013, 10:23 AM
Spanky_10
How to convert a digit to words
I am trying to figure out how to convert digits from 0 to 9 to words in my program.
What is the proper code to do this?

I have found examples online but none of them are working with my code.
What is just a generic example? I need to do it inside a switch statement too.

Code Java:

```switch (operation) { case "+": System.out.println("The sum of "); }```

I need to take my input of numbers from the user as digits and change them to words in this system.out statement.
• March 5th, 2013, 10:33 AM
Norm
Re: How to convert a digit to words
Quote:

convert digits from 0 to 9 to words
If the digits are int values, make an array of the Strings that correspond to the int values (eg 0 = "zero")
and use the int value to index into the array to get the corresponding String.
• March 5th, 2013, 03:57 PM
Spanky_10
Changing digits to words
Code Java:

```import java.util.*;   public class StudentInformation { public static void main (String [] args) { Scanner scan = new Scanner (System.in);   System.out.println("Please enter the first number between 0 and 9: "); int num1 = scan.nextInt();   System.out.println("\nPlease enter the second number between 0 and 9: "); int num2 = scan.nextInt();   System.out.println("\nPlease enter your operator: + for ADDITION, - for SUBTRACTION, * for MULTIPLICATION,"); System.out.println("/ for DIVISION, or ^ for EXPONENTIATION: "); String operation = scan.next();   if (num1 < 0 || num1 > 9 || num2 < 0 || num2 > 9) { System.out.println("\nInvalid Number"); }   NumberToWords num = new NumberToWords();   switch (operation) { case "+": System.out.println("The sum of " + num.convert(num1) + " and " + num.convert(num2) + " is " + (num1 + num2)); break; case "-": System.out.println("The difference of "); break; case "*": System.out.println("The product of "); break; case "/": System.out.println("The quotient of "); break; case "^": System.out.println("The expontentiation of "); break; default: System.out.println(operation + " is not valid."); }   } }```

I am not sure how to write an array and have looked it up. Where is it supposed to be located at in my program? I need to convert the user input integers into words for the system.out statements in the switch loop.
Thanks
• March 5th, 2013, 04:06 PM
Norm
Re: How to convert a digit to words
Quote:

Where is it supposed to be located at in my program?
If everything in the program is in the main() method, you can put it in the main() method.
• March 5th, 2013, 09:20 PM
Robertgif
Re: Changing digits to words
Quote:

Originally Posted by Spanky_10
Code Java:

```  if (num1 < 0 || num1 > 9 || num2 < 0 || num2 > 9) { System.out.println("\nInvalid Number"); }```

This is not too valid to your question, but think about moving this if() statement, you have it after you imput both the first and the second number, so the user is not sure which number they imputed incorrectly. You could always make two if you need to.

To write an array you have to first declare it:

Code Java:

```int[] myIntArray = new int[3]; // This will give you an array that holds integers. // The 3 means that it will hold three values, you can then assign your values:   myIntArray[0] = 1; myIntArray[1] = 2; myIntArray[2] = 3; // This will then assign the values 1,2,3 to the array positions 0,1,2 // So when you do:   System.out.print(myIntArray[0]); //You will get 1.```

Try and think about how you can use this to get words.
This is just a basic way of declaring arrays.
• March 6th, 2013, 12:19 PM
Norm
Re: How to convert a digit to words