number formatting using recursion

Reply

Join Date: Mar 2005
Posts: 2
Reputation: statc is an unknown quantity at this point 
Solved Threads: 0
statc statc is offline Offline
Newbie Poster

number formatting using recursion

 
0
  #1
Mar 9th, 2005
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: number formatting using recursion

 
0
  #2
Mar 10th, 2005
why complicate things?

  1. import java.text.NumberFormat;
.......
  1. static String insertCommas(int num)
  2. {
  3. return NumberFormat.getInstance().format(num);
  4. }
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: number formatting using recursion

 
0
  #3
Mar 10th, 2005
I can help you, but I have school right now, but after I'll help you.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC