0

I am having a bit of a problem here. I am trying to reverse a user inputted string using recursion, but I keep getting an error. Basically, a parameterless void, recursive function should be called to print out the string backwards. I get an error on the 23rd line only under the reverse word, which states "The method reverse() in the type RecursionReversal is not applicable for the arguments (String)". There are quick fixes to it given in Eclipse, but none of them are what I need. Am I missing something?

import java.util.Scanner;

public class RecursionReversal
{
    public static String origChars;

    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter 5 characters:");
        origChars= input.nextLine();        

        reverse();
    }

    public static void reverse()
    {
        if(origChars.length() <= 1)
            return;
        else
            reverse(origChars.substring(1) + origChars.charAt(0));
    }
}
1
  • 1
    It would be nice to know what that error is. Commented Nov 17, 2014 at 22:50

5 Answers 5

2

Add an argument to the reverse method to correspond to the method call being made within the method

public static void reverse(String input)
Sign up to request clarification or add additional context in comments.

Comments

1

Your method should take a String argument; using a static field might work but has a bad code smell. Anyway, take the last character in the input String and then substring everything else. Like,

public static String reverse(String str) {
    if (str.length() <= 1)
        return str;
    else
        return Character.toString(str.charAt(str.length() - 1))
                + reverse(str.substring(0, str.length() - 1));
}

If you must do it with the global, it could be done with something similar but you must update origChars before you call reverse() (without arguments) like

private static String origChars;
public static void reverse() {
    if (origChars.length() <= 1)
        System.out.print(origChars);
    else {
        System.out.print(origChars.charAt(origChars.length() - 1));
        origChars = origChars.substring(0, origChars.length() - 1);
        reverse();
    }
}

3 Comments

That's what I had, then I realized that I need to use a parameterless void function.
@evarias Look at the my second version, notice the else requires updating origChars; and reverse() can't take arguments.
it works but I thought that for recursion, there needed to be an argument inside the reverse function?
0

You are not defining parameters or a return type for the reverse function. To use recursion I would recommend replacing your reverse method with

public static String reverse(String s)
{
    if(s.length() <= 1)
        return s;
    else
        reverse(s.substring(1) + s.charAt(0));
}

You have the right idea with the reverse(s.substring(1) + s.charAt(0)); statement but you need a return type otherwise you have no way to get the result. Secondly, without a parameter, the method has no way to know what part of the String to reverse.

Lastly, to make it work, from your main, call origichars = reverse(origChars); instead of calling reverse();.

2 Comments

Yep I understand that part, but I was trying to find a way to do this with a parameterless void function.
I just added an answer that does with a parameterless void function.
0

Here's a way to do this with a parameterless void function. reverse() takes off the first char, calls reverse() then puts the char back on the end of origChars.

public static void reverse()
{
    if(origChars.length() <= 1)
        return;
    char first = origChars.charAt(0);
    origChars = origChars.substring(1);
    reverse();
    origChars+=first;
}

Comments

0

Try this:

public String reverseUsingRecursion(String str){
    if(str.length() == 0 || str.length() == 1){
        return str;
    }   
    String reverseString = reverseUsingRecursion(str.substring(1)) 
                           + str.charAt(0); 
    return reverseString;          
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.