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!