• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi Gentlemen (I want your help)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic