• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion with Strings  RSS feed

 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, so I just started learning about recursion and I was trying to get this sample of code to work properly:




However, it keeps giving me an error telling me that non-static method printString cannot be referenced from static context. However, I know that the second method has to stay void and its other code must stay the way it is. I am simpily trying to get the output of the code, however, even after searching online and reading on recursion, I still do not completely know what I am doing wrong.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:
However, it keeps giving me an error telling me that non-static method printString cannot be referenced from static context.


The printString() method is a non-static method. To call that method, you need an instance. The main() method is a static method. It doesn't have a this reference.... so, if the main() method needs to call the printString() method, it needs to provide an instance.

And BTW, line 5 is also not valid Java syntax.

Henry
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried changing it like this:



but its telling me void type is not allowed here with ("wyo") highlighted. I could've sworn thats how you create an instance. What am I doing wrong?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually scratch that, I think I may have just figured it out. I changed it to this:



and now it printed out "oyw" which I think is right.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for following up with the final result. Here's a cow...

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!