1

I am trying to use a method to reverse the characters in a string and I keep getting a type mismatch error. Any thoughts?

public static String userReverse (String userEntry3) {
    String reverse = "";    
       for (int i = (userEntry3.length() -1); i >= 0 ; i--) {
       reverse = System.out.println(userEntry3.charAt(i));
    }
    return reverse;
    }

4 Answers 4

2

System.out.println is a void method. It returns nothing. So it cannot assigned back to a String variable

Your code is wrong.

If you want to reverse a string, you can use this:

public static String userReverse (String userEntry3) {
    return new StringBuilder(userEntry3).reverse().toString()
}
Sign up to request clarification or add additional context in comments.

2 Comments

Of course in practice we would do this, but maybe the OP needs to implement his own reversing logic.
Wow. StringBuilder::reverse had no idea this method existed before now :P
0

Get rid of System.out.println and add a += to concatenate the new char

public static String userReverse (String userEntry3) {
    String reverse = "";    
    for (int i = (userEntry3.length() -1); i >= 0 ; i--) {
        reverse += userEntry3.charAt(i);
    }
    return reverse;
}

EDIT: As Tim said in the comments, StringBuilder can be used too (and is better practice than concatenating strings in a loop):

public static String userReverse (String userEntry3) {
    StringBuilder reverse = new StringBuilder();    
    for (int i = (userEntry3.length() -1); i >= 0 ; i--) {
        reverse.append(userEntry3.charAt(i));
    }
    return reverse.toString();
}

1 Comment

Still there is no explanation, why StringBuilder is better and what is wrong with System.out.println() Poor programming(er) instance.
0

A more optimized way to reverse a string includes two pointer approach: Use one pointer to start from the beginning and the other to start from the end. By the time they meet each other your string is already reversed

public static String userReverse (String userEntry3) {

    int i = 0;
    int j = userEntry3.length()-1;
    StringBuilder myName = new StringBuilder(userEntry3);

    for(; i < j ; i++,j--){
        char temp = userEntry3.charAt(i);
        myName.setCharAt(i,userEntry3.charAt(j));
        myName.setCharAt(j,temp);
    }
    return myName.toString();

}

Comments

0

System.out.println() is a void method and it not return anything. you should try it this way,

public static String userReverse (String userEntry3) {
String reverse = "";    
  for (int i = (userEntry3.length() -1); i >= 0 ; i--) {
   reverse += userEntry3.charAt(i).toString();
}
return reverse;
}

1 Comment

Of course, This is a simplest thing to do in this situation.

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.