hello... i had a code like this a while back... it is supposed to reverse whatever string you have entered using recusion...
i think its right.. but the compiler doesnt...
it returns an error saying invalid method declaration; return type required...
sorry if i don't get it and have to pester you guys wid a trivial matter...
Your main() method doesn't have a return type declared.... you forgot the "void".
[EDIT] Wasn't paying attention. It looks like this isn't the main method called by the JVM, but one that returns a String. So, I guess you need to declare that.
[EDIT 2] BTW, I don't see any recursion here. Perhaps you didn't show us everything. And given the call to JOptionPane where it is, I don't see how this can ever be recursion -- no matter what the rest of the program looks like.
Oh, I see what you are doing.... It looks like you forgot about the reverse() method -- you seemed to have taken the logic that was once in a reverse() method, and inserted it into the main() method.... Can't do that. You need that code in the reverse method, or the code can't call the reverse() method recursively.
so what do i need to do???
sorry but i really, really am new to java...i still don't know what to do... i've tried removing brackets and stuff... but the error remained the same...
Peachy Manasis wrote:so what do i need to do???
sorry but i really, really am new to java...i still don't know what to do... i've tried removing brackets and stuff... but the error remained the same...
Why don't you simply go back to where you cut-n-pasted this code, and this time, get the whole method?
Henry
Jeanne Boyarsky
,
author & internet detective
staff
Peachy Manasis wrote:so what do i need to do???
sorry but i really, really am new to java...i still don't know what to do... i've tried removing brackets and stuff... but the error remained the same...
Recursion is an advanced topic. If you are still learning how the braces work, it would be best to use a loop to reverse the String until you learn more about java.
*sigh!*
That's why i dont know what to do... that was the last idea i had in mind....
using recursion without charAt, subString AND stringbuffer..... *sigh*
Peachy Manasis wrote:
That's why i dont know what to do... that was the last idea i had in mind....
using recursion without charAt, subString AND stringbuffer..... *sigh*
This is why Stephan asked you to define "recursion" for us. From your interaction so far, it doesn't seem like you know what it is. I recommend that you throw away the code the you got "a while back", and start from scratch -- maybe start from your class text book. You need to first understand what recursion is before you start implementing.
And as Jeanne mentioned, recursion is an advanced topic. There are a ton of subtleties which need to be understood. You really need to understand it cold, or you won't be able to use it, during your advanced studies.
The trick in recursion is to assume the method that you are writing is already implemented, and it works correctly, so you can use a call to that method to help you actually implement it.
Say you are writing a method that looks like this:
Now, just assume that if you were to call this method, it already magically worked. You can use this to implement the actual method:
There is a small problem here. The method will keep calling itself indefinitely, because no work is done on the string. So the trick is to do a little bit of work on the string, and make it simpler, before you pass it to the next call of reverse.
What is the simplest string you can think of to reverse?
it didn't say that charArrays are forbidden.. so i guess i'll try...
all i could manage was this... with many, many .class expected errors and such like...
i know theyre just errors on my part... but i don't seem to know what to do with them...
ooooh... i know they ARE a lot of errors....
Post by:autobot
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, the permaculture playing cards