943,548 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 429
  • Java RSS
Sep 7th, 2009
0

Please Help!

Expand Post »
Convert Number to Words
Kindly Help me Explain this Line of Codes.
Line by Line if possible.
It was given to me by my friend.

Hoping for Some Help.
Thank you very much.

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. public class WordNumber{
  3. public static void main(String[]args){
  4. for(int i = 0; i < 20; i++){
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Enter a Non Floating Point Number: ");
  7. long x = input.nextLong();
  8. System.out.println(makeWord(x)); //Please Explain
  9. }//end for
  10. }//end main
  11.  
  12. //Please Explain
  13. private static final String[] numbers = {
  14. "zero","one","two","three","four","five","six",
  15. "seven","eight","nine","ten","eleven","twelve",
  16. "thirteen","fourteen","fifteen","sixteen","seventeen",
  17. "eighteen","nineteen"};
  18. private static final String[] tens = {
  19. "","teen","twenty","thirty","fourty","fifty",
  20. "sixty","seventy","eighty","ninety"};
  21.  
  22. //Please Explain
  23. public static String makeWord(long number){
  24. String s = "";
  25.  
  26. if(number<0){
  27. s="negative ";
  28. number*=-1;
  29. }
  30.  
  31. s+=toWord(number);
  32. s = s.replace("-zero","");
  33.  
  34. return s;
  35. }//end of makeWord
  36.  
  37. //Please Explain
  38. private static String toWord(long number){
  39.  
  40. if(number>=1000000000){
  41. return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000);
  42. }
  43. else if(number>=1000000){
  44. return toWord(number/1000000)+"-million-"+toWord(number%1000000);
  45. }
  46. else if(number>=1000){
  47. return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000);
  48. }
  49. else if(number>=100){
  50. return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);//Please Explain
  51. }
  52. else if(number>=20){
  53. return tens[(int)number/10] + "-" + toWord(number%10);
  54. }
  55. else if(number>18){
  56. return numbers[(int)number%10]+"teen";
  57. }
  58. else{
  59. return numbers[(int)number];
  60. }
  61.  
  62. }//end of toWord
  63.  
  64. }//end class
Reputation Points: 10
Solved Threads: 0
Unverified User
fox07 is offline Offline
4 posts
since Sep 2009
Sep 7th, 2009
0

Re: Please Help!

Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?

In any case, if you had done your assignment, rather than copying it, you would probably understand it.

P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 7th, 2009
0

Re: Please Help!

Click to Expand / Collapse  Quote originally posted by masijade ...
Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?

In any case, if you had done your assignment, rather than copying it, you would probably understand it.

P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.
well, i'm new in java.
he explained some like this.
its a final variable which is String[] numbers and String[] tens.
static it is executed according to its appearance.
correct?
Java Syntax (Toggle Plain Text)
  1. private static final String[] numbers = {
  2. "zero","one","two","three","four","five","six",
  3. "seven","eight","nine","ten","eleven","twelve",
  4. "thirteen","fourteen","fifteen","sixteen","seventeen",
  5. "eighteen","nineteen"};
  6. private static final String[] tens = {
  7. "","teen","twenty","thirty","fourty","fifty",
  8. "sixty","seventy","eighty","ninety"};

here's a static initializers which being executed in order.
right?
Java Syntax (Toggle Plain Text)
  1. public static String makeWord(long number){
  2. String s = "";
  3.  
  4. if(number<0){
  5. s="negative ";
  6. number*=-1;
  7. }
  8.  
  9. s+=toWord(number);
  10. s = s.replace("-zero","");
  11.  
  12. return s;
  13. }//end of makeWord
  14.  
  15. private static String toWord(long number){
  16.  
  17. if(number>=1000000000){
  18. return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000);
  19. }
  20. else if(number>=1000000){
  21. return toWord(number/1000000)+"-million-"+toWord(number%1000000);
  22. }
  23. else if(number>=1000){
  24. return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000);
  25. }
  26. else if(number>=100){
  27. return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);
  28. }
  29. else if(number>=20){
  30. return tens[(int)number/10] + "-" + toWord(number%10);
  31. }
  32. else if(number>18){
  33. return numbers[(int)number%10]+"teen";
  34. }
  35. else{
  36. return numbers[(int)number];
  37. }
  38.  
  39. }//end of toWord

kinda confused.
Reputation Points: 10
Solved Threads: 0
Unverified User
fox07 is offline Offline
4 posts
since Sep 2009
Sep 7th, 2009
0

Re: Please Help!

adamson university?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Sep 9th, 2009
0

Re: Please Help!

Try to give a proper description to the thread.

Don't put "Please Help" as the thread.

Obviously everybody here are going to help you, if you show your effort.

And if you are not putting any effort no body will help you obviously.

So try to put the proper Title for the Threads you post in future.
Reputation Points: 40
Solved Threads: 2
Light Poster
sbhavan is offline Offline
45 posts
since Oct 2008

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.
Message:
Previous Thread in Java Forum Timeline: how to display a java message box on top of all active windows
Next Thread in Java Forum Timeline: HELP!!!(please read this..)





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


Follow us on Twitter


© 2011 DaniWeb® LLC