• 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

Recursion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i implement recursion in a try catch block?

I tried using the THIS keyword but it did not work.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Individual statements can't be recursive - only entire methods can.
 
zain hoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried recursion in the entire method, but it seems that due to the try catch block in that method, there seems to be some problem.
Is that possible?
Any way out of it?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's impossible to tell what problems a method might have the source of which we can't see. In other words: post the source code (and please UseCodeTags when doing so).
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
zain hoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what errors do you get?

Please tell the details - if you're only saying "it doesn't work", or "there is some error" then you are making it difficult for people to help you solve your problem.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 21 will lead to either an infinite loop or a StackOverflowError; I think it's the latter. The reason is quite clear: you called move with two arguments. An exception occurs. What do you do? You call the method with the same two, unmodified arguments. As a result, the same exception will occur, and you call the method with the same two, unmodified arguments. As a result, the same exception will occur, etc.
Line 29 will also cause the infinite loop or a StackOverflowError, since you're again calling the same method with the same, unmodified arguments.

In recursion, you usually call the same method, but with different arguments. Somewhere in the method it will perform a check that will lead to a path that does not call the same method, thereby ending the recursion.
 
zain hoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to sort out that problem.
Wat i did was make two variables static, that solved the problem.

Thanks for the help guys
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which two variables are static? Why did you make them static? That changes the behaviour of the variable, which may cause you problems later.
Changing the design of your class as a workaround because a control structure doesn't work sounds very suspect to me.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic