0

I got this message: the left-hand side of an assignment must be a variable when i finished to resolve an excercise about printing a string reversely in recursively-looping. I just want to know if someone could provide an explanation of it? The error message appears in the last line...I don't understand, WHY? There is my code:

import java.util.Scanner;

public class Excersise {

    public static void main(String[] args) {

        // Create a Scanner
        Scanner input = new Scanner(System.in);
        //Prompt the user to enter a string
        System.out.print("Enter a string: ");
        String s = input.nextLine();
        reverseDisplay(s);

    }

    public static void reverseDisplay(String value) {

        //Base case
        if (value.length() < 2) { 
            System.out.println(value);
        } else {
            //Recursion
            reverseDisplay(value.substring(1)) + value.charAt(0); <--error

        }
    }
}
5
  • 3
    reverseDisplay's return type is void. What does it mean to add a void to a char? Commented Feb 16, 2014 at 22:04
  • review this line again reverseDisplay(value.substring(1)) + value.charAt(0); <--error ... you add a void to a char !! Commented Feb 16, 2014 at 22:05
  • 1
    I think you should replace that last line with two lines - reverseDisplay(value.substring(1)); and then System.out.print(value.charAt(0)); and also change println to print in the if branch. Commented Feb 16, 2014 at 22:08
  • thx david Wallace, but it returns the string in 2 parts! Commented Feb 16, 2014 at 22:16
  • It works perfectly, i understand now what i did wrong: NO ADDING A VOID TO A CHAR!! - Thanks everyone Commented Feb 16, 2014 at 22:24

2 Answers 2

1
import java.util.Scanner;
public class Exercise {
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
        //Prompt the user to enter a string
        System.out.print("Enter a string: ");
        String s = input.nextLine();
        reverseDisplay(s);
        System.out.println();
    }
    public static void reverseDisplay(String value) {
        //Base case
        if (value.length() < 2) { 
            System.out.print(value);
        } else {
            //Recursion
            //calls for string without first char
            reverseDisplay(value.substring(1));
            //prints first char
            System.out.print(value.charAt(0));
        }
    }
}

This works. Your approach to recursion was on the right track, but you want to print the first character after calling the method on the rest of the string. I also added another println at the end of main so the reversed string would appear on its own line.
The compiler error you got was because the compiler thought that the line was supposed to be an assignment (likeint a = b + c) and didn't see an =.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to change return type of your method to String and return it and print in your main method

public class Exercise{

public static void main(String[] args) {

    // Create a Scanner
    Scanner input = new Scanner(System.in);
    //Prompt the user to enter a string
    System.out.print("Enter a string: ");
    String s = input.nextLine();
    System.out.println(reverseDisplay(s));

}

public static String reverseDisplay(String value) {

    //Base case
    if (value.length() < 2) { 
        return value;
    } else {
        //Recursion
        return reverseDisplay(value.substring(1)) + value.charAt(0);

    }
}

}

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.