-3

I am trying to take the user input from one method and use it in another. I am confused about the error because they are both of type int.


public static void move()
{
    System.out.println("What do you want to do?");
    Scanner scan = new Scanner(System.in);
    int userMove = scan.nextInt();
}

public static void usersMove(String playerName, int gesture)
{
    int userMove = userMove.move(); //error is here

    if (userMove == -1)
    {
        break;
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2181402
  • 87
  • 2
  • 2
  • 8

2 Answers2

6
int userMove = userMove.menu();

userMove here is an int(primitive data type). How can you call a method on that?

I'm guessing that you want something like this:-

public static int move()
{
    System.out.println("What do you want to do?");
    Scanner scan = new Scanner(System.in);
    int userMove = scan.nextInt();
    return userMove;
}

public static void usersMove(String playerName, int gesture)
{
    int userMove = move(); //Now error will go.

    if (userMove == -1)
    {
        break;
    }
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Just guessing, since it is not clear what userMove is... but you seem to use a variable from another method in the caller. You need to return it to do that

public static int move()
{
   System.out.println("What do you want to do?");
   Scanner scan = new Scanner(System.in);
   return scan.nextInt();
}

public static void usersMove(String playerName, int gesture)
{
   int userMove = move(); //error is here

   if (userMove == -1)
   {
       break;
   }
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77