Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reversing a string using recursion

 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if i declare it void... there won't be no return....

*sorry!*
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stephan van Hulst
Bartender
Pie
Posts: 5401
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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*
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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*...
 
Stephan van Hulst
Bartender
Pie
Posts: 5401
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a letter??? say... a...
 
Stephan van Hulst
Bartender
Pie
Posts: 5401
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh.... you call it and then put the next element before(?) it???
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original code would have worked just fine, if you would have put that code in the reverse method.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sadly... we aren't allowed to use charAt and/or subString...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic