2

I have a method that asks for the user's name, then returns it. Now I want to access the user's name in another method, but when I print the user's name it says "null". I don't understand, I defined the variable, but how can I give it access across the entire class?

public static String userName() {
        Scanner input = new Scanner (System.in);

        System.out.print("Hello! Welcome to The Game. Whats your name? ");
        String userName = input.next();

        return userName;
}

This is the method where I try to access the userName variable but am given "null"

public static void homeMethod() {
        Scanner input = new Scanner (System.in);

        System.out.println("Hello " + userName + "! What would you like to do? (Type LIST for options)");
        String userGameChoice = input.nextLine();
}

Calling the userName() method inside the homeMethod() i am given the same error.

Any help is much appreciated. Thanks!

4
  • 2
    You need to put the variable in the class. Commented Jul 25, 2018 at 21:26
  • You need to call the method. Commented Jul 25, 2018 at 21:28
  • Thanks! I already had it there but realized I created a new variable in the userName() method, so I had two userName variables. Commented Jul 25, 2018 at 21:30
  • geeksforgeeks.org/variable-scope-in-java Commented Jul 25, 2018 at 22:10

3 Answers 3

1

I needed to create userName variable outside of the method.

This is what I had:

public class MainGameClass {
    public static String userName;

    public static String userName() {
    Scanner input = new Scanner (System.in);

    System.out.print("Hello! Welcome to The Game. Whats your name? ");
    String userName = input.next();

    return userName;
    }

    public static void homeMethod() {
    Scanner input = new Scanner (System.in);

    System.out.println("What would you like to do " + userName + "? (Type LIST for options)");
    String userGameChoice = input.nextLine();
    }
}

Output when calling homeMethod():

What would you like to do null? (Type LIST for options)

Solution:

public class MainGameClass {
    public static String userName;

    public static String userName() {
    Scanner input = new Scanner (System.in);

    System.out.print("Hello! Welcome to The Game. Whats your name? ");
    String userName = input.next();

    return userName;
    }

    public static void homeMethod() {
    Scanner input = new Scanner (System.in);

    System.out.println("What would you like to do " + userName + "? (Type LIST for options)");
    userGameChoice = input.nextLine();
    }
}

Output when calling homeMethod():

What would you like to do (userName)? (Type LIST for options)

Explanation:

I correctly defined the userName variable in the class, however, in the userName() method, I created a new variable under the same name. Therefore not returning the answer to the userName variable.

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

1 Comment

this answer would be better if you have provided some code to explain it
0

Either declare userName variable outside the method, in the class itself or call userName method in your homeMethod like this:

public static void homeMethod() {
    Scanner input = new Scanner (System.in);
    System.out.println("Hello " + userName() + "! What would you like to do? (Type LIST for options)");
    String userGameChoice = input.nextLine();

}

Comments

0

You have to put userName out of the method like this:

System.out.print("Hello! Welcome to The Game. Whats your name? ");
String userName = input.next()

public static String userName() {
    Scanner input = new Scanner (System.in);
    return userName;
}

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.