wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes reversing a string using recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reversing a string using recursion" Watch "reversing a string using recursion" New topic
Author

reversing a string using recursion

Peachy Manasis
Greenhorn

Joined: Dec 04, 2010
Posts: 22
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peachy Manasis
Greenhorn

Joined: Dec 04, 2010
Posts: 22
but if i declare it void... there won't be no return....

*sorry!*
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40


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

Joined: Dec 04, 2010
Posts: 22
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peachy Manasis
Greenhorn

Joined: Dec 04, 2010
Posts: 22
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

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Dec 04, 2010
Posts: 22
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

Joined: Dec 04, 2010
Posts: 22
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Dec 04, 2010
Posts: 22
*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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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

Joined: Dec 04, 2010
Posts: 22
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

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Dec 04, 2010
Posts: 22
a letter??? say... a...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Dec 04, 2010
Posts: 22
oh.... you call it and then put the next element before(?) it???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Your original code would have worked just fine, if you would have put that code in the reverse method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peachy Manasis
Greenhorn

Joined: Dec 04, 2010
Posts: 22
sadly... we aren't allowed to use charAt and/or subString...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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

Joined: Dec 04, 2010
Posts: 22
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....
 
Consider Paul's rocket mass heater.
 
subject: reversing a string using recursion
 
Similar Threads
Swing Program with Buttons
charAt problem....
counting instance of numbers in a particular string
Place A Word In A 2d Array
String Tokenizer Crypting.