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 < 10)
{
output += ones(0);
}
else if(input < 20)
{
output += teens(0);
}
else if(input < 100)
{
output += tens(0);
output += ones(1);
}
else if(input < 1000)
{
output += hundreds(0);
int temp = Integer.parseInt(rawInput.substring(1,3));
if(temp > 10 && temp < 20)
{
output += teens(2);
}
else
{
output += tens(1);
output += ones(2);
}
}
else if(input < 10000)
{
output += thousands(0);
output += hundreds(1);
int temp = Integer.parseInt(rawInput.substring(2,4));
if(temp > 10 && temp < 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 < 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 < 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 < 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";
}
}