patternjavaMinor
Digit to words converter
Viewed 0 times
digitwordsconverter
Problem
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 && 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",
```
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 && 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",
Solution
int input = Integer.parseInt(rawInput);This can throw an exception if rawInput isn't a number.
else if(input 10 && temp < 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 > 1000)
{
output += thousands;
subtract thousands from input;
}
if (input > 100)
{
output += hundreds;
subtract hundreds from input;
}
if (10 = 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".
Code Snippets
int input = Integer.parseInt(rawInput);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);
}
}if (input > 1000)
{
output += thousands;
subtract thousands from input;
}
if (input > 100)
{
output += hundreds;
subtract hundreds from input;
}
if (10 < input < 20)
{
output += teens;
}
else
{
if (input >= 20)
{
output += tens;
subtract tens from input;
}
output += ones;
}
if (output is empty)
output = "Zero";int temp = Integer.parseInt(rawInput.substring(1,3));main(args);Context
StackExchange Code Review Q#39707, answer score: 7
Revisions (0)
No revisions yet.