title: Number to Word Conversion in Java
body: <p>I have this homework assignment for this class I'm retaking, the problem I'm running into is that I'm over-thinking the solution. I have to create a program that converts a four digit number to  words.</p>
  
  <p>(Example: 1134 becomes "One One Three Four")</p>
  
  <p>I have a basic code, but it's bulky and ugly. I'm also only allowed to use basic if and switch statements, we have to use a switch statement as well.</p>
  
  <p>Am I over thinking this? I can't figure out how to make this code shorter and I only want to use one switch statement without a while loop. Is it even possible or is this as short as it gets.</p>
  
  <p>Here's my code.
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import java.util.Scanner;
  
  public class NumberToWords {
  
   public static void main(String[] args) {
    //Set up scanner.
    Scanner kb = new Scanner(System.in);
    
    //Ask for a 4 digit integer.
    System.out.println("Enter a 4 digit number.");
    
    //Store 4 digit number into a variable
    int number = kb.nextInt();
    
    //Seperate number into digits.
    int digit4 = number%10;
    number = number/10;
    int digit3 = number%10;
    number = number/10;
    int digit2 = number%10;
    number = number/10;
    int digit1 = number%10;
    number = number/10;
    
    //Set up a switch statement to read through the number.
    switch (digit1)
    {
    case 1: System.out.print("One ");break;
    case 2: System.out.print("Two "); break;
    case 3: System.out.print("Three "); break;
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five "); break;
    case 6: System.out.print("Six "); break;
    case 7: System.out.print("Seven "); break;
    case 8: System.out.print("Eight "); break;
    case 9: System.out.print("Nine "); break;
    case 0: System.out.print("Zero "); break;
    default: System.out.print(""); break;
    }
    switch (digit2)
    {
    case 1: System.out.print("One ");break;
    case 2: System.out.print("Two "); break;
    case 3: System.out.print("Three "); break;
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five "); break;
    case 6: System.out.print("Six "); break;
    case 7: System.out.print("Seven "); break;
    case 8: System.out.print("Eight "); break;
    case 9: System.out.print("Nine "); break;
    case 0: System.out.print("Zero "); break;
    default: System.out.print(""); break;
    }
    switch (digit3)
    {
    case 1: System.out.print("One ");break;
    case 2: System.out.print("Two "); break;
    case 3: System.out.print("Three "); break;
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five "); break;
    case 6: System.out.print("Six "); break;
    case 7: System.out.print("Seven "); break;
    case 8: System.out.print("Eight "); break;
    case 9: System.out.print("Nine "); break;
    case 0: System.out.print("Zero "); break;
    default: System.out.print(""); break;
    }
    switch (digit4)
    {
    case 1: System.out.print("One ");break;
    case 2: System.out.print("Two "); break;
    case 3: System.out.print("Three "); break;
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five "); break;
    case 6: System.out.print("Six "); break;
    case 7: System.out.print("Seven "); break;
    case 8: System.out.print("Eight "); break;
    case 9: System.out.print("Nine "); break;
    case 0: System.out.print("Zero "); break;
    default: System.out.print(""); break;
    }
   }
  
  }</code></pre>
  </div>
  </div>
  </p>