Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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";
- }
- }
- 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);
- public static void main(String[] args)
- {
- while (true)
- run();
- }
- static void run()
- {
- rawInput = JOptionPane.showInputDialog("Enter a number between 0 and 9999.");
- ... etc ...
- }
- String getInput() {
- return JOptionPane.showInputDialog("Enter a number between 0 and 9999. q to Quit");
- }
- //...
- String rawInput = getInput();
- while (!rawInput.equal("q") {
- //...
- rawInput = getInput();
- }
- private static String suffix(String input, int index, String[] suffixes)
- {
- int x = Character.getNumericValue(input.charAt(index));
- for(int i = 0; i < suffixes.length; i++)
- {
- if(i == x)
- {
- return " " + suffixes[i];
- }
- }
- }
- interface Converter {
- String convert(int value);
- }
- Map<Integer, Converter> converters = new LinkedHashMap<>();
- converters.put(1, new ZeroConverter());
- converters.put(10, new OnesConverter());
- //...
- for (Entry<Integer, Converter> e : converters.entrySet()) {
- if (input < e.getKey()) {
- output = e.getValue().convert(input);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement