0

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.

static String insertCommas(int num)
{
         int rem=0;
         String str="";
        if(num<1000)
                        {
                        str+=num;  
                        return str;
                        }
                        else
                        {
                        str+=num+",";
                        insertCommas(num%1000);
                        }
                       return str;
}
3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by server_crash
0

why complicate things?

import java.text.NumberFormat;

.......

static String insertCommas(int num)
{
		 return NumberFormat.getInstance().format(num);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.