Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. package numberwords2;
  2. import java.awt.Component;
  3. import javax.swing.JOptionPane;
  4. public class NumberWords2
  5. {
  6. static String rawInput;
  7. static String output = "";
  8. static int hold;
  9.  
  10. private static Component frame;
  11.  
  12. public static void main(String[] args)
  13. {
  14.  
  15. rawInput = JOptionPane.showInputDialog("Enter a number between 0 and 9999.");
  16. int input = Integer.parseInt(rawInput);
  17.  
  18. if(input == 0)
  19. {
  20. output += "Zero";
  21. }
  22. else if(input < 10)
  23. {
  24. output += ones(0);
  25. }
  26. else if(input < 20)
  27. {
  28. output += teens(0);
  29. }
  30. else if(input < 100)
  31. {
  32. output += tens(0);
  33. output += ones(1);
  34. }
  35. else if(input < 1000)
  36. {
  37. output += hundreds(0);
  38. int temp = Integer.parseInt(rawInput.substring(1,3));
  39. if(temp > 10 && temp < 20)
  40. {
  41. output += teens(2);
  42. }
  43. else
  44. {
  45. output += tens(1);
  46. output += ones(2);
  47. }
  48.  
  49. }
  50. else if(input < 10000)
  51. {
  52. output += thousands(0);
  53. output += hundreds(1);
  54. int temp = Integer.parseInt(rawInput.substring(2,4));
  55. if(temp > 10 && temp < 20)
  56. {
  57. output += teens(3);
  58. }
  59. else
  60. {
  61. output += tens(2);
  62. output += ones(3);
  63. }
  64.  
  65. }
  66.  
  67. JOptionPane.showMessageDialog(frame, rawInput + " is" + output + ".");
  68. output = "";
  69. main(args);
  70. }
  71.  
  72. public static String teens(int hold)
  73. {
  74. String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
  75. "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nighteen"};
  76. int x = Character.getNumericValue(rawInput.charAt(hold));
  77. int y = 0;
  78. for(int i = 0; i < 10; i++)
  79. {
  80. if(i == x)
  81. {
  82. y = i;
  83. break;
  84. }
  85. }
  86. return " " + teens[y];
  87. }
  88.  
  89. private static String tens(int hold)
  90. {
  91. String[] tens = {"", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
  92. int x = Character.getNumericValue(rawInput.charAt(hold));
  93. int y = 0;
  94. for(int i = 0; i < 10; i++)
  95. {
  96. if(i == x)
  97. {
  98. y = i;
  99. }
  100. }
  101. return " " + tens[y];
  102. }
  103.  
  104. private static String ones(int hold)
  105. {
  106. String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
  107. int x = Character.getNumericValue(rawInput.charAt(hold));
  108. int y = 0;
  109. for(int i = 1; i < 10; i++)
  110. {
  111. if(i == x)
  112. {
  113. y = i;
  114. break;
  115. }
  116. }
  117. return " " + ones[y];
  118. }
  119.  
  120. private static String hundreds(int hold)
  121. {
  122. return ones(hold) + " Hundred";
  123. }
  124.  
  125. private static String thousands(int hold)
  126. {
  127. return ones(hold) + " Thousand";
  128. }
  129. }
  130.  
  131. int input = Integer.parseInt(rawInput);
  132.  
  133. else if(input < 10)
  134. {
  135. output += ones(0);
  136. }
  137. else if(input < 20)
  138. {
  139. output += teens(0);
  140. }
  141. else if(input < 100)
  142. {
  143. output += tens(0);
  144. output += ones(1);
  145. }
  146. else if(input < 1000)
  147. {
  148. output += hundreds(0);
  149. int temp = Integer.parseInt(rawInput.substring(1,3));
  150. if(temp > 10 && temp < 20)
  151. {
  152. output += teens(2);
  153. }
  154. else
  155. {
  156. output += tens(1);
  157. output += ones(2);
  158. }
  159.  
  160. }
  161.  
  162. if (input > 1000)
  163. {
  164. output += thousands;
  165. subtract thousands from input;
  166. }
  167. if (input > 100)
  168. {
  169. output += hundreds;
  170. subtract hundreds from input;
  171. }
  172. if (10 < input < 20)
  173. {
  174. output += teens;
  175. }
  176. else
  177. {
  178. if (input >= 20)
  179. {
  180. output += tens;
  181. subtract tens from input;
  182. }
  183. output += ones;
  184. }
  185. if (output is empty)
  186. output = "Zero";
  187.  
  188. int temp = Integer.parseInt(rawInput.substring(1,3));
  189.  
  190. main(args);
  191.  
  192. public static void main(String[] args)
  193. {
  194. while (true)
  195. run();
  196. }
  197.  
  198. static void run()
  199. {
  200. rawInput = JOptionPane.showInputDialog("Enter a number between 0 and 9999.");
  201. ... etc ...
  202. }
  203.  
  204. String getInput() {
  205. return JOptionPane.showInputDialog("Enter a number between 0 and 9999. q to Quit");
  206. }
  207.  
  208. //...
  209.  
  210. String rawInput = getInput();
  211. while (!rawInput.equal("q") {
  212. //...
  213. rawInput = getInput();
  214. }
  215.  
  216. private static String suffix(String input, int index, String[] suffixes)
  217. {
  218. int x = Character.getNumericValue(input.charAt(index));
  219. for(int i = 0; i < suffixes.length; i++)
  220. {
  221. if(i == x)
  222. {
  223. return " " + suffixes[i];
  224. }
  225. }
  226. }
  227.  
  228. interface Converter {
  229. String convert(int value);
  230. }
  231.  
  232. Map<Integer, Converter> converters = new LinkedHashMap<>();
  233. converters.put(1, new ZeroConverter());
  234. converters.put(10, new OnesConverter());
  235. //...
  236.  
  237. for (Entry<Integer, Converter> e : converters.entrySet()) {
  238. if (input < e.getKey()) {
  239. output = e.getValue().convert(input);
  240. break;
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement