'While compilation it shows error at int rand = rand.nextInt(3);

I'm creating a Rock Paper Scissors game using Java. During compilation, it throws an error for generating a random number. I use a random class but still, it throws an error.

This is the error:

Multiple markers at this line
   Cannot invoke nextInt(int) on the primitive 
   type int
   -Duplicate local variable rand
import java.util.Random;
import java.util.Scanner;
  
public class rockpaperscissors {
    
    public static void main(String[] args) {
        
         System.out.println("Welcome to Rock Paper Scissors Game");
        Scanner sc=new Scanner(System.in);
        while(true) {
       System.out.print("What is your move? Choose any of the following r rock, paper, or scissors ");
      System.out.print("To quit the game, enter quit");
      Scanner sc1=new Scanner(System.in);
      String Mymove=sc.nextLine();
      System.out.print("If you want to quit , Please write QUIT");
      if(Mymove.equals("QUIT")) {
          break;
      }
           
            
            if(!Mymove.equals("ROCK") || !Mymove.equals("SCISSORS") || !Mymove.equals("PAPER"))
            {System.out.print("Invalid");
            }
            else {
                
                System.out.print("Proceed");
            }
            
            Random rand = new Random();
             int rand = rand.nextInt(3); //in this section error occurs. Can you please suggest me   
                
             String opponentMove = "";
           if( rand==0)
               {opponentMove="ROCK";
               }
  else if(rand==1)
       {opponentMove="PAPER";
       }
       
       else {
           opponentMove="SCISSORS";
       
       }
           
            if(Mymove.equals(opponentMove))
            {
                System.out.println("Tie!");
            }
            else if(Mymove.equals("ROCK") && opponentMove.equals("SCISSORS"))
            {System.out.println("You are Won! Congrats ");
            
            }
            
            else if(Mymove.equals("PAPER") && opponentMove.equals("ROCK"))
                    {System.out.println("Computer Won! Congrats ");
                    }
                    
            else if(Mymove.equals("PAPER") && opponentMove.equals("SCISSORS"))
                    {
                System.out.println("Computer Won! Congrats ");
                    }
                    }

            
        
        }

    
    }
}


Solution 1:[1]

You are declaring "rand" 2 types as "Random" class object, and "int(Integer)" object. Just copy paste these 2 lines in your code:

Random randObj = new Random();
int rand = randObj.nextInt(3);

Solution 2:[2]

The duplicate variable name is pretty straightforward - you have two variables named rand. This is simple to repair - rename one of the following.

   Random rand = new Random();
   int rand = rand.nextInt(3); 

The other compilation error is probably a side effect of the first. The compiler cannot determine which rand you are trying to invoke nextInt(int) on so it opts for the wrong one. Solve the first problem and this one goes away.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rvgala29
Solution 2 vsfDawg