944,091 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2851
  • Java RSS
Mar 9th, 2005
0

number formatting using recursion

Expand Post »
hi, i was wondering if anyone could help me out? i have this project in my java class where i need to format an integer to a string with commas (ex:1000000->1,000,000) using recursion. I've tried it for 4 class periods and i just can't figure it out. If you feel guilty by giving me just the answer feel free to give me a tutorial on recursion so that i get the answer and understand it better.
I figured some people woudl like to see what i have now.

Java Syntax (Toggle Plain Text)
  1. static String insertCommas(int num)
  2. {
  3. int rem=0;
  4. String str="";
  5. if(num<1000)
  6. {
  7. str+=num;
  8. return str;
  9. }
  10. else
  11. {
  12. str+=num+",";
  13. insertCommas(num%1000);
  14. }
  15. return str;
  16. }
Last edited by statc; Mar 9th, 2005 at 10:22 pm. Reason: added code
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
statc is offline Offline
2 posts
since Mar 2005
Mar 10th, 2005
0

Re: number formatting using recursion

why complicate things?

Java Syntax (Toggle Plain Text)
  1. import java.text.NumberFormat;
.......
Java Syntax (Toggle Plain Text)
  1. static String insertCommas(int num)
  2. {
  3. return NumberFormat.getInstance().format(num);
  4. }
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 10th, 2005
0

Re: number formatting using recursion

I can help you, but I have school right now, but after I'll help you.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

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: Executable program design question....
Next Thread in Java Forum Timeline: Question about URLConnection





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


Follow us on Twitter


© 2011 DaniWeb® LLC