• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hi Gentlemen (I want your help)  RSS feed

 
bole bolat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very happy found this place in net because I
want to learn more and more about java.

Thanks alot for who established and help us in
this place.
.....................................................
.....................................................
I faced one prolem when I solve my HW which is to add
commes to an integer # using recursion
ex. 1234 to 1,2,3,4

I did :

public static int Comma(int n)
{
if(s==1)
return s;
else
//return Comma (s.charAt(n-1))+","+charAt(n);
return Comma (s.substrin(n-2,n-1))+","+charAt(n);
}
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The nature of a recursive solution requires that the method returns intermediate results at each step. So the return type (and its parameter type as well) should be String, not int.
It's also important that at each step the nested invocation of Comma has a simpler parameter than the enclosing invocation, e.g. Comma("1234") should lead to Comma("123"). (It would be more common to reduce it to Comma("234") instead of what your code does, but both ways are possible).

I won't go into it any deeper since this is a homework that you should be doing. Come back here if you have more questions.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!