Home > Software design >  How do i pass the scanner input of one method as a parameter for another method without being prompt
How do i pass the scanner input of one method as a parameter for another method without being prompt

Time:10-02

I have to refer to the users input name in the second method an I don't know if it is possible . What i have right prompts me to enter the name agin. also sorry if this is a dumb question i just started to code

public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);  
 getName(keyboard); 
 getRounds(keyboard,getName());

public static String getName(Scanner keyboard) {
System.out.print("Welcome to ROCK PAPER SCISSORS. I, Computer, will be your opponent.\n");
System.out.print("Please type in your name and press return: ");
String name = keyboard.next();
System.out.print("\nWelcome "   name   ".");

return name;
 }


public static int getRounds(Scanner keyboard, String getName) {
 System.out.println();
 System.out.print("\nAll right" getName  "How many rounds would you like to play?\n");

 System.out.print("Enter the number of rounds you want to play and press return: ");

 int rounds = keyboard.nextInt();

 return rounds;

}

CodePudding user response:

It's because you're calling your getName method twice. You're passing in the getName method instead of your string value from the getName method.

CodePudding user response:

Try String name = getName(keyboard); getRounds(keyboard,name);

  • Related