Forums Register Login

reversing a string using recursion

+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.

Henry
+Pie Number of slices to send: Send
but if i declare it void... there won't be no return....

*sorry!*
+Pie Number of slices to send: Send

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.

Henry
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
our instruction said we MUST use recursion... and we hadn't had the time to discuss it since our instructor had to go away for a while...
+Pie Number of slices to send: Send
In that case...

Can you first describe what recursion is for us? Describe in your own words how you could reverse a word using recursion.
+Pie Number of slices to send: Send
ummm... lessee... i usually think of recursion as a factorial translated into code...
*that's how i'd like to think of it....*

as for describing the code....
im not really good at explaining...

since in my program your input will be printed as is if your input is null(no input) or just a single string...
so if input is two or more.....

oh no... i just checked the instructions again... and it said we cannot use charAt and subString... waaaaaaaaaahhhh!!! what do i do???
+Pie Number of slices to send: Send
i made a new one widout charAt and subString... it goes like this...



i just don't get how it doesn't look like a recursion...
or so my friend says...
+Pie Number of slices to send: Send
I'm fairly certain you won't be allowed to use StringBuffer.reverse, either.

Recursion means that a method calls itself - where do you think that happens in your code?
+Pie Number of slices to send: Send
*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*
+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
and there appears my real problem....
*sigh*... i guess you guys are right....

i thought i'd sample a recursion first... and analyze it later...
guess i can't....
*sigh*...
+Pie Number of slices to send: Send
Well don't give up yet.

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?
+Pie Number of slices to send: Send
a letter??? say... a...
+Pie Number of slices to send: Send
Exactly!

So, what can you do with a given string of any length, to make it more easy for the next call of reverse to solve?
+Pie Number of slices to send: Send
oh.... you call it and then put the next element before(?) it???
+Pie Number of slices to send: Send
Your original code would have worked just fine, if you would have put that code in the reverse method.
+Pie Number of slices to send: Send
sadly... we aren't allowed to use charAt and/or subString...
+Pie Number of slices to send: Send
So how are you supposed to get the first and/or last character? For reversing a String you need to split it up.

Unless you are allowed to use toCharArray. Call that, reverse the char[], then create a new String from that.
+Pie Number of slices to send: Send
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....
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
https://gardener-gift.com


reply
reply
This thread has been viewed 3655 times.
Similar Threads
String Tokenizer Crypting.
counting instance of numbers in a particular string
charAt problem....
Place A Word In A 2d Array
Swing Program with Buttons
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:22:03.