Hi i am creating a lottery program and i have managed to get it so the user enters 6 numbers into the program then it will generate random numbers the problem is that the numbers the user enteres can be duplicate i need help trying to not make them duplicates.here is my code so far

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class LotteryProgram {

    public static void main(String[] args) {

        Scanner user_input = new Scanner (System.in);
        Scanner keyIn = new Scanner(System.in);

        int[] LotteryNumbers = new int[6];
        int input;
        int count = 0;

        System.out.print("Welcome to my lottery program please select how many weeks you wish to play /nand the numbers you wish to use!");
        Scanner Weeks_Played = new Scanner(System.in);

        String Weeks_Play;
        System.out.print ("Enter the amount of weeks you wish to play for: ");
        Weeks_Play = Weeks_Played.next();

        System.out.println ("You are playing for 3 weeks");
        System.out.print("\n\nPress the enter key to continue");
        keyIn.nextLine();


        for (int i = 0; i < LotteryNumbers.length; i++)
        {
            count ++;

            System.out.println("Enter your five Lottery Numbers now " + count + " (must be between 1 and 49): ");
            input = Integer.parseInt(user_input.next());


            if (input < 1 || input > 49)
            {
                while (input < 1 || input > 49)
                {
                    System.out.println("Invalid number entered! \nPlease enter lottery number (between 1 and 49) " + count);
                    input = Integer.parseInt(user_input.next());

                    if (input >= 1 || input <= 49)
                    {
                     LotteryNumbers[i] = input;
                    }
                }
            }
            else
            {
            LotteryNumbers[i] = input;
            }
        }

        System.out.println("Thank you for your numbers.\nThe system will now check if you have any matching numbers");
        System.out.print("Press the enter key to continue");
        keyIn.nextLine();

        Random randNumGenerator = new Random();
        StringBuilder output = new StringBuilder();

        int[] ActLotteryNumbers = new int[6];

            for (int j = 0; j < ActLotteryNumbers.length; j++)
                {
                int roll = randNumGenerator.nextInt(49);
                ActLotteryNumbers[j] = roll;
                }

            System.out.println(Arrays.toString(ActLotteryNumbers));

        int counter = 0;
        for (int i = 0; i < LotteryNumbers.length; i++)
        {
            for (int j = 0; j < ActLotteryNumbers.length; j++)
            {
             if (LotteryNumbers[i] == ActLotteryNumbers [j])
             {
                 counter ++;
                 System.out.println("The numbers that match up are: \n" + LotteryNumbers[i]);
             }
            }
        }
        if (counter == 0)
        {
          System.out.println("You had no matching numbers this week ... Try Again next week!");
        }
      }
    }

I was thinking of using this piece of small code i found but dont know how to put into the code above without causing problems

{
    public Set<Integer> LotteryNumbers()
    {
        Set<Integer> RandomNumbers = new HashSet<Integer>();

        Random random = new Random();
        for (int i=0; i<6; i++)
        RandomNumbers.add(random.nextInt(49));

        return RandomNumbers;
    }
}

Recommended Answers

All 5 Replies

just keep a list of numbers already added, and compare each new number with that list.

Any idea on how to go about this?

can create an private that find if exist...
so maybe...

private boolean isAlreadyExists(int [] anActLoteryNumbers, int newNumber){
//search if exist in the list.
    for(int i = 0; i < anActLoteryNumbers.length; i++){
        if(anActLoteryNumbers[i]==newNumber){
            return true;
        }
    }
    return false;
}

when u validate if exists is FALSE can save this value in the
ActLotteryNumbers[j] = roll;
IF EXISTS (true) can try again.
basically in the lines ... 64-68

 for (int j = 0; j < ActLotteryNumbers.length; j++){
      int roll = randNumGenerator.nextInt(49);
      if(isAlreadyExists(ActLotteryNumbers, roll)){//if exists (true)
        j--;//try again
      }else{//if not exists (false)
        ActLotteryNumbers[j] = roll;
      }  
  }

I expect help you. think more about ur solution.

correction implementation. don't pass the array as an argument. the array should be on instance level (or, in this case: on class level, so make it static), so it doesn't have to be passed.
the new method also has to be static, unless you plan to start working with instantiating your class and taking your code out of the main method.

u have reason! i forget it. use static method out of main method.

private static boolean isAlreadyExists(...){
//...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.