This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String display without any loops

 
yuva rani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to display a string 'n' number of times without using any loops. here 'n' is user entered integer number.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try recurssion.

If you do a search of the ranch, I am sure there are plenty of topics mentioning recurssion, and plenty on the web in general.
[ May 14, 2008: Message edited by: Gavin Tranter ]
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea, Gavin.

And welcome to JavaRanch, Yuva Rani.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuva,

Here is a recursive example that might help you for your problem. Keep in mind this is just one possible solution (a loop is a much better idea though since recursion creates a bunch of calls on the stack and can often be harder to understand).



Hope this helps,
Jeff
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess its a matter of preference, but I normally find recursion easier to understand than loops.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Storey:
Yuva,

Here is a recursive example that might help you for your problem. Keep in mind this is just one possible solution (a loop is a much better idea though since recursion creates a bunch of calls on the stack and can often be harder to understand).



Hope this helps,
Jeff


Jeff i think you missed one small thing in your code


instead of
 
yuva rani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for answering my question!!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Storey:
Here is a recursive example that might help you for your problem.


Jeff - basically, you just did this guy's homework for him. At JavaRanch we ask LetThemDoTheirOwnHomework.

By all means give hints (e.g. "you could consider recursion"), but not fully -worked answers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic