Please Help!

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

Join Date: Sep 2009
Posts: 4
Reputation: fox07 is an unknown quantity at this point 
Solved Threads: 0
fox07 fox07 is offline Offline
Newbie Poster

Please Help!

 
0
  #1
Sep 7th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Please Help!

 
0
  #2
Sep 7th, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: fox07 is an unknown quantity at this point 
Solved Threads: 0
fox07 fox07 is offline Offline
Newbie Poster

Re: Please Help!

 
0
  #3
Sep 7th, 2009
Originally Posted by masijade View Post
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?
  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?
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 45
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: Please Help!

 
0
  #4
Sep 7th, 2009
adamson university?
a joke.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 38
Reputation: sbhavan is an unknown quantity at this point 
Solved Threads: 1
sbhavan's Avatar
sbhavan sbhavan is offline Offline
Light Poster

Re: Please Help!

 
0
  #5
Sep 9th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 231 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC