Digit to words converter This is one of the longest programs I have made with methods and I think I must be doing this rather inefficiently. Any comments to improve would be appreciated.

package numberwords2;
import java.awt.Component;
import javax.swing.JOptionPane;
public class NumberWords2
{
    static String rawInput;
    static String output = "";
    static int hold;

    private static Component frame;

    public static void main(String[] args)
    {

        rawInput = JOptionPane.showInputDialog("Enter a number between 0 and 9999.");
        int input = Integer.parseInt(rawInput);

        if(input == 0)
        {
            output += "Zero";
        }
        else if(input &lt; 10)
        {
            output += ones(0);
        }
        else if(input &lt; 20)
        {
            output += teens(0);
        }
        else if(input &lt; 100)
        {
            output += tens(0);
            output += ones(1);
        }
        else if(input &lt; 1000)
        {
            output += hundreds(0);
            int temp = Integer.parseInt(rawInput.substring(1,3));
            if(temp &gt; 10 &amp;&amp; temp &lt; 20)
            {
                output += teens(2);
            }
            else
            {
                output += tens(1);
                output += ones(2);
            }

        }
        else if(input &lt; 10000)
        {
            output += thousands(0);
            output += hundreds(1);
            int temp = Integer.parseInt(rawInput.substring(2,4));
            if(temp &gt; 10 &amp;&amp; temp &lt; 20)
            {
                output += teens(3);
            }
            else
            {
                output += tens(2);
                output += ones(3);
            }

        }

        JOptionPane.showMessageDialog(frame, rawInput + " is" + output + ".");
        output = "";
        main(args);
    }

    public static String teens(int hold)
    {
        String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
            "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nighteen"};
        int x = Character.getNumericValue(rawInput.charAt(hold));
        int y = 0;
        for(int i = 0; i &lt; 10; i++)
        {
            if(i == x)
            {
                y = i;
                break;
            }
        }
        return " " + teens[y];
    }

    private static String tens(int hold)
    {
        String[] tens = {"", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        int x = Character.getNumericValue(rawInput.charAt(hold));
        int y = 0;
        for(int i = 0; i &lt; 10; i++)
        {
            if(i == x)
            {
                y = i;
            }
        }
        return  " " + tens[y];
    }

    private static String ones(int hold)
    {
        String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        int x = Character.getNumericValue(rawInput.charAt(hold));
        int y = 0;
        for(int i = 1; i &lt; 10; i++)
        {
            if(i == x)
            {
                y = i;
                break;
            }
        }
        return " " + ones[y];
    }

    private static String hundreds(int hold)
    {
        return ones(hold) + " Hundred";
    }

    private static String thousands(int hold)
    {
        return ones(hold) + " Thousand";
    }
}
int input = Integer.parseInt(rawInput);


This can throw an exception if rawInput isn't a number.



    else if(input &lt; 10)
    {
        output += ones(0);
    }
    else if(input &lt; 20)
    {
        output += teens(0);
    }
    else if(input &lt; 100)
    {
        output += tens(0);
        output += ones(1);
    }
    else if(input &lt; 1000)
    {
        output += hundreds(0);
        int temp = Integer.parseInt(rawInput.substring(1,3));
        if(temp &gt; 10 &amp;&amp; temp &lt; 20)
        {
            output += teens(2);
        }
        else
        {
            output += tens(1);
            output += ones(2);
        }

    }


Code (subroutine calls) are duplicated in the above:


If the number is less than 1000 then you call tens, teens, and ones
If the number is greater than 1000 then you call tens, teens, and ones


There would be less code if you processed it from left to right, like you do when you read and say a number:

if (input &gt; 1000)
{
    output += thousands;
    subtract thousands from input;
}
if (input &gt; 100)
{
    output += hundreds;
    subtract hundreds from input;
}
if (10 &lt; input &lt; 20)
{
    output += teens;
}
else
{
    if (input &gt;= 20)
    {
        output += tens;
        subtract tens from input;
    }
    output += ones;
}
if (output is empty)
    output = "Zero";




int temp = Integer.parseInt(rawInput.substring(1,3));


This is a complicated way to remove the thousands from the input.

A simpler way to subtract thousands from the input is:

input = input - ((input / 1000) * 1000);



main(args);


This is a strange way to loop forever.

Better would be:

public static void main(String[] args)
{
    while (true)
        run();
}

static void run()
{
    rawInput = JOptionPane.showInputDialog("Enter a number between 0 and 9999.");
    ... etc ...
}




Perhaps only the first letter should be capitalized; perhaps with hyphens; and the word "and"; for example "Four hundred and forty-three" not "Four Hundred Forty Three".