Search code examples
javamethodsreturn

Return a string to main method


first off sorry if this question has been asked before (if it has i couldn't find it)

this is my code

public class Methods {

    public static void main(String[] args) {
        String playerOne = "this is blank";
        lols(playerOne);
        System.out.println(playerOne);
    }

    
    public static String lols(String playerOne) {
        playerOne = "this is filled";
        return playerOne;
        
    }
}

I want the playerOne String to change to "this is filled" but when it prints it says "this is blank"

i am unsure why this code is not working.

thanks for the help.


Solution

  • you already are returning the String, you are just not doing anything with the result.

    Change this:

    public static void main(String[] args) {
        String playerOne = "this is blank";
        lols(playerOne);
        System.out.println(playerOne);
    }
    

    to either this:

    public static void main(String[] args) {
        String playerOne = "this is blank";
        playerOne = lols(playerOne);
        System.out.println(playerOne);
    }
    

    or to this, and directly use the returned value without storing it:

    public static void main(String[] args) {
        String playerOne = "this is blank";
        System.out.println(lols(playerOne));
    }