how to convert digit(number) into word

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 10
Reputation: sach_ak47 is an unknown quantity at this point 
Solved Threads: 0
sach_ak47 sach_ak47 is offline Offline
Newbie Poster

how to convert digit(number) into word

 
0
  #1
Jan 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,205
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: how to convert digit(number) into word

 
0
  #2
Jan 15th, 2007
you have to "hard code it" with use of array for example
  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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: how to convert digit(number) into word

 
0
  #3
Jan 15th, 2007
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: how to convert digit(number) into word

 
0
  #4
Jan 17th, 2007
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
Western New England College '08
Computer Science

Programming Lang's:

C, C#, Java, Lisp, MASM, Visual Basic 6
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: c_shaft05 is an unknown quantity at this point 
Solved Threads: 1
c_shaft05's Avatar
c_shaft05 c_shaft05 is offline Offline
Junior Poster in Training

Re: how to convert digit(number) into word

 
0
  #5
Jan 18th, 2007
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);
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: aniseed is an unknown quantity at this point 
Solved Threads: 6
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: how to convert digit(number) into word

 
0
  #6
Jan 18th, 2007
Originally Posted by c_shaft05 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: c_shaft05 is an unknown quantity at this point 
Solved Threads: 1
c_shaft05's Avatar
c_shaft05 c_shaft05 is offline Offline
Junior Poster in Training

Re: how to convert digit(number) into word

 
0
  #7
Jan 21st, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: jimmylee is an unknown quantity at this point 
Solved Threads: 0
jimmylee jimmylee is offline Offline
Newbie Poster
 
-1
  #8
Oct 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #9
Oct 10th, 2009
Originally Posted by jimmylee View Post
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: toorkish is an unknown quantity at this point 
Solved Threads: 0
toorkish toorkish is offline Offline
Newbie Poster

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

 
-2
  #10
9 Days Ago
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...

--------------------------------
  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. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC