943,605 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 46747
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 15th, 2007
0

how to convert digit(number) into word

Expand Post »
Hi guys i am new here
i want to help about:
how to convert digit(number) into word
for example:
Enter a number:123
Output:one hundred twenty-three
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
sach_ak47 is offline Offline
10 posts
since Dec 2006
Jan 15th, 2007
-2

Re: how to convert digit(number) into word

you have to "hard code it" with use of array for example
Java Syntax (Toggle Plain Text)
  1. String[] myArray={"zero", "one", two", "three"};
  2. int num = 0;
  3. System.out.println(myArray[num]);
  4. num = 3;
  5. System.out.println(myArray[num]);
  6.  

first return will be string zero and second one will be three
Last edited by peter_budo; Jan 15th, 2007 at 8:30 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,653 posts
since Dec 2004
Jan 15th, 2007
0

Re: how to convert digit(number) into word

which won't provide a correct solution in almost any situation
You'll need a lot more logic than that, but it could be used as a starting point.

Instead of an array though you should really consider using an enum.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 17th, 2007
0

Re: how to convert digit(number) into word

This is going to be harder than you think due to the fact that numbers are represented differently depending on the position in the number the specific digit is in. For example, if a 5 is in the one's slot, it is represented as "five", whereas in the ten's spot it is fifty-[ones digit number(unless zero)], then back to "five" for the hundreds and so on.

In a nutshell, you would need a fair amount of logic as jwenting stated, just figured I would elaborate some more.

Regards,

Tyler S. Breton
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Jan 18th, 2007
0

Re: how to convert digit(number) into word

I would approach this making an ArrayList of strings instead of a list (tad more versitile, but list of Strings will do as well) and then create a string variable that you can do x = x + number.toString();

Perhaps some if statements will solve the other have of the problem

if (number ==1) then list.get(n-1);
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
c_shaft05 is offline Offline
58 posts
since Jul 2005
Jan 18th, 2007
0

Re: how to convert digit(number) into word

Click to Expand / Collapse  Quote originally posted by c_shaft05 ...
making an ArrayList of strings instead of a list (tad more versitile ...
How so? An ArrayList is a List, unless you mean to use ArrayList instead of an array of Strings.
Reputation Points: 48
Solved Threads: 7
Posting Whiz
aniseed is offline Offline
353 posts
since Apr 2006
Jan 21st, 2007
0

Re: how to convert digit(number) into word

I mis spoke... I had some python crossing my java and things were fuzzy... but you are right... Array List instead of an Array of strings..
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
c_shaft05 is offline Offline
58 posts
since Jul 2005
Oct 10th, 2009
-2
Re: how to convert digit(number) into word
How do I convert the Integer to String by:
1. Asking the user to input the number (Eg: 704 = Seven Hundred and Four)

2. Display the output in JTextArea.

Thanks for your support.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimmylee is offline Offline
2 posts
since Oct 2009
Oct 10th, 2009
1
Re: how to convert digit(number) into word
Click to Expand / Collapse  Quote originally posted by jimmylee ...
How do I convert the Integer to String by:
1. Asking the user to input the number (Eg: 704 = Seven Hundred and Four)

2. Display the output in JTextArea.

Thanks for your support.
Start a new thread. This is a 3 year old thread.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 25th, 2009
-2

Converting integers into strings - bank cheque assignment - 1st year university

HIYA GUYS
VERY BASIC WAY OF DOING IT AND ONLY HANDLES UP TO NUMBER 999 ALTHOUGH CAN BE ALTERED PRETTY EASILY.

THIS CODE DOES NOT USE CONFUSING ARRAYS WHICH WOULD CUT DOWN MY CODE A LOT BUT THIS CODE IS THE FULL WHACK AND THE WAY THEY WOULD TEACH SOMEONE AT UNIVERSITY AT BASIC LEVEL.

COPY AND PASTE INTO JAVA EDITOR...

OBVIOUSLY A GOOD WAY TO UNDERSTAND MY CODE PROPERLY OR ANY CODE IS TO USE THE DEBUG OPTION IN UR JAVA EDITOR TO GO THROUGH THE STEPS ONE BY ONE FOR ANY GIVEN NUMBER

THANKS BYE...

--------------------------------
Java Syntax (Toggle Plain Text)
  1.  
  2. // Program to convert integers into words, Ex5.5
  3. package num2word;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9. // units Method
  10. public static void units(int n) {
  11.  
  12. switch (n) {
  13. case 1:
  14. System.out.print("one ");
  15. break;
  16. case 2:
  17. System.out.print("two ");
  18. break;
  19. case 3:
  20. System.out.print("three ");
  21. break;
  22. case 4:
  23. System.out.print("four ");
  24. break;
  25. case 5:
  26. System.out.print("five ");
  27. break;
  28. case 6:
  29. System.out.print("six ");
  30. break;
  31. case 7:
  32. System.out.print("seven ");
  33. break;
  34. case 8:
  35. System.out.print("eight ");
  36. break;
  37. case 9:
  38. System.out.print("nine ");
  39. break;
  40. }
  41. }
  42.  
  43. // special case Method
  44. public static void special(int n) {
  45.  
  46. switch (n) {
  47. case 11:
  48. System.out.print(" eleven");
  49. break;
  50. case 12:
  51. System.out.print(" twelve");
  52. break;
  53. case 13:
  54. System.out.print(" thirteen");
  55. break;
  56. case 14:
  57. System.out.print(" fourteen");
  58. break;
  59. case 15:
  60. System.out.print(" fifteen");
  61. break;
  62. case 16:
  63. System.out.print(" sixteen");
  64. break;
  65. case 17:
  66. System.out.print(" seventeen");
  67. break;
  68. case 18:
  69. System.out.print(" eighteen");
  70. break;
  71. case 19:
  72. System.out.print(" nineteen");
  73. break;
  74. }
  75. }
  76.  
  77. // tens Method
  78. public static void tens(int n) {
  79.  
  80. switch (n) {
  81. case 1:
  82. System.out.print(" ten ");
  83. case 2:
  84. System.out.print(" twenty ");
  85. break;
  86. case 3:
  87. System.out.print(" thirty ");
  88. break;
  89. case 4:
  90. System.out.print(" forty ");
  91. break;
  92. case 5:
  93. System.out.print(" fifty ");
  94. break;
  95. case 6:
  96. System.out.print(" sixty ");
  97. break;
  98. case 7:
  99. System.out.print(" seventy ");
  100. break;
  101. case 8:
  102. System.out.print(" eighty ");
  103. break;
  104. case 9:
  105. System.out.print(" ninety ");
  106. break;
  107. }
  108. }
  109.  
  110. // hundreds Method
  111. public static void hundreds(int n) {
  112.  
  113. switch (n) {
  114. case 1:
  115. System.out.print("one hundred ");
  116. break;
  117. case 2:
  118. System.out.print("two hundred ");
  119. break;
  120. case 3:]
  121. System.out.print("three hundred ");
  122. break;
  123. case 4:
  124. System.out.print("four hundred ");
  125. break;
  126. case 5:
  127. System.out.print("five hundred ");
  128. break;
  129. case 6:
  130. System.out.print("six hundred ");
  131. break;
  132. case 7:
  133. System.out.print("seven hundred ");
  134. break;
  135. case 8:
  136. System.out.print("eight hundred ");
  137. break;
  138. case 9:
  139. System.out.print("nine hundred ");
  140. break;
  141. }
  142. }
  143.  
  144. public static void main(String[] args) {
  145. Scanner input = new Scanner(System.in);
  146.  
  147. // Requesting User input
  148. System.out.print("Enter a number between 0 and 999: ");
  149. int number = input.nextInt();
  150.  
  151. // input validation
  152. while (number < 0 || number > 999) {
  153. System.out.println("Input Too Large, ");
  154. System.out.print("Enter a number between 0 and 999: ");
  155. number = input.nextInt();
  156. }
  157.  
  158. // Below we start to determine what the input was by
  159. // categorising it using boolean
  160.  
  161. // original number (100-999)
  162. if (number > 99 && number < 1000) {
  163. int h = number / 100; //find the hundreds only (one, two hundred)
  164. hundreds(h); //print number
  165.  
  166. // first part of number found & printed (hundreds)
  167. // remamining 2 digits must be found ie tens & units
  168.  
  169. // test if they are a special number
  170. int x = 0; // initialized variable x for calculations
  171. x = number % 100; // find remainder of hundreds (ie x54)
  172. if (x > 10 && x < 20) { // ie is 54 a special number?
  173. special(x); // print number
  174. }
  175. // if not special number -> split up into tens & units
  176. if (x > 0 && x < 100) {
  177.  
  178. int tens = x / 10; // finding the tens (forty, fifty)
  179. tens(tens); // print number
  180.  
  181. int units = x % 10; // finding the units (one, two)
  182. units(units); // print number
  183. }
  184. }
  185.  
  186. // original number (20-99)
  187. if (number > 19 && number < 100) {
  188.  
  189. int t = number / 10; // finding the tens (forty, fifty)
  190. tens(t); // print number
  191.  
  192. int u = number % 10; // finding the units (one, two)
  193. units(u); // print number
  194. }
  195.  
  196. // original number (11-19) (Special numbers)
  197. if (number > 10 && number < 20) { // (Special numbers)
  198. special(number); // print number
  199. }
  200.  
  201. // original number (0-9)
  202. if (number < 10) { // finding the units (one, two)
  203. units(number); // print number
  204. }
  205. }
  206. }
Reputation Points: 6
Solved Threads: 0
Newbie Poster
toorkish is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Thread Alive
Next Thread in Java Forum Timeline: Snake Speeds up after the Menu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC