Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program that prompts a user for k distinct lottery numbers within

ID: 3763957 • Letter: I

Question

I am writing a program that prompts a user for k distinct lottery numbers within n range 1-n.

The numbers are entered as elements in an array. Of course, the numbers must be greater than zero. Also, for simplicity sake, n can be no larger than 100. Also, the k numbers must all be less than n.

Anyway, the issue I'm having is those k numbers must be distinct. So, the array must searched for duplicates and the duplicates must be replaced. I want the user to receive an error notice immediately after he/she enters a duplicate number (i.e., array element). I then want the user to be immediately prompted for another number, which of course, must also be distinct among all the numbers already in the array and all those that may be entered.

The basic gist of my question is this: How do I write a method that 1. accepts an array of integer numbers, 2. checks array for duplicate(s), 3. notifies the user of duplicate number(s) and prompts for replacement(s), 4. replaces duplicate in array with new replacement and checks once more for duplicates, and so on and so on until all elements in the array are distinct?

I have code, and I feel I'm close but I'm still receiving duplicates in the return array. So, there must be a bug somewhere.

My code:

_______________________________________________________________________

public class LotteryPowerball
{
    public static void LotteryTicket (String[] args)
    {
        Scanner input = new Scanner(System.in);
        boolean errCorr;
        int n;
        int k;
        int answer;
       
        do
        {
            do
            {
                System.out.println ("Enter n range of non-Powerball numbers 1 to n: ");
                n = input.nextInt();
                if (n <= 0)
                {
                    System.out.println ("Negative entry. Try again.");
                    System.out.println();
                }
                else if (n > 100)
                {
                    System.out.println ("Careful. Number is too large. No one wins if the Lotto Machine breaks down! Try again.");
                    System.out.println();
                }
            } while (n <= 0 || n > 100);
            System.out.println();
       
            do
            {
                System.out.println ("Enter number of k distinct integers: ");
                k = input.nextInt();
                if (k <= 0)
                {
                    System.out.println ("Playing less than one number? Makes no sense. You wanna play, don't you? Try again.");
                    System.out.println();
                }
                else if (k > n)
                {
                    System.out.println ("Ha! " + k + " lotto numbers in a range of " + n + "? You wish. Try again.");
                    System.out.println();
                }
            } while (k <= 0 || k > n);
            System.out.println();   
            int[] arrayPlayer = new int[k];
       
            System.out.println ("Pick your lottery numbers: ");
            for (int i = 0; i < arrayPlayer.length; i++)
            {
                arrayPlayer[i] = input.nextInt();
                if (arrayPlayer[i] > n)
                {
                    System.out.println ("Lotto number is outside range.");
                    System.out.println ("Try again: ");
                    i--;
                }
                else if (arrayPlayer[i] <= 0)
                {
                    System.out.println ("Lotto number less than 1? Eh.");
                    System.out.println ("Try again: ");
                    i--;
                }  
            }
            duplicateNumbers(arrayPlayer, n, k);
            System.out.println();
            System.out.println (arrayPlayer[2]);
            System.out.println();
            System.out.println ("Pick your powerball number: ");
            int m = input.nextInt();
            System.out.println();
            System.out.println ("Lottery numbers selected: ");
            for (int i = 0; i < k - 1; i++)
            {
                System.out.print (arrayPlayer[i] + ", ");
            }
            System.out.println (arrayPlayer[k - 1]);
            System.out.println();
           
            System.out.println ("Powerball number selected: ");
            System.out.println (m);
            System.out.println();
            System.out.println ("Enter 1 to quit or any other key to play again: ");
            answer = input.nextInt();
        } while (answer != 1);
    }
    public static void duplicateNumbers (int[] array, int n, int k)
    {
        Scanner input = new Scanner(System.in);
        int duplicateReplace;
        for (int i = 0; i < array.length; i++)
        {
            for (int j = i + 1; j < array.length; j++)
            {
                if (j != i && array[i] == array[j])
                {
                    System.out.println ("Lottery requires " + k + " distinct numbers.");
                    System.out.println ("Please select a replacement lottery number for " + array[i] + ": ");
                    duplicateReplace = input.nextInt();
                      
                    int temp = array[i];
                    array[i] = duplicateReplace;
                    duplicateReplace = temp;
                       
                    System.out.println();
                }
            }
        }
       
           
    }
}

______________________________________________________________

Any help is appreciated.

Thanks!

Explanation / Answer

import java.util.Scanner;

public class LotteryPowerball {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       boolean errCorr;
       int n;
       int k;
       int answer;

       do {
           do {
               System.out
                       .println("Enter n range of non-Powerball numbers 1 to n: ");
               n = input.nextInt();
               if (n <= 0) {
                   System.out.println("Negative entry. Try again.");
                   System.out.println();
               } else if (n > 100) {
                   System.out
                           .println("Careful. Number is too large. No one wins if the Lotto Machine breaks down! Try again.");
                   System.out.println();
               }
           } while (n <= 0 || n > 100);
           System.out.println();

           do {
               System.out.println("Enter number of k distinct integers: ");
               k = input.nextInt();
               if (k <= 0) {
                   System.out
                           .println("Playing less than one number? Makes no sense. You wanna play, don't you? Try again.");
                   System.out.println();
               } else if (k > n) {
                   System.out.println("Ha! " + k
                           + " lotto numbers in a range of " + n
                           + "? You wish. Try again.");
                   System.out.println();
               }
           } while (k <= 0 || k > n);
           System.out.println();
           int[] arrayPlayer = new int[k];

           System.out.println("Pick your lottery numbers: ");
           for (int i = 0; i < arrayPlayer.length; i++) {
               arrayPlayer[i] = input.nextInt();
               if (arrayPlayer[i] > n) {
                   System.out.println("Lotto number is outside range.");
                   System.out.println("Try again: ");
                   i--;
               } else if (arrayPlayer[i] <= 0) {
                   System.out.println("Lotto number less than 1? Eh.");
                   System.out.println("Try again: ");
                   i--;
               }
           }
           arrayPlayer = duplicateNumbers(arrayPlayer, n, k);
           System.out.println();
           System.out.println(arrayPlayer[2]);
           System.out.println();
           System.out.println("Pick your powerball number: ");
           int m = input.nextInt();
           System.out.println();
           System.out.println("Lottery numbers selected: ");
           for (int i = 0; i < k - 1; i++) {
               System.out.print(arrayPlayer[i] + ", ");
           }
           System.out.println(arrayPlayer[k - 1]);
           System.out.println();

           System.out.println("Powerball number selected: ");
           System.out.println(m);
           System.out.println();
           System.out
                   .println("Enter 1 to quit or any other key to play again: ");
           answer = input.nextInt();
       } while (answer != 1);
   }

   public static int[] duplicateNumbers(int[] array, int n, int k) {
       Scanner scanner = new Scanner(System.in);
       int result[] = new int[array.length], j = 0;
       for (int i = 0; i < array.length; i++) {
           int kOld = array[i];
           if (!isExists(result, kOld)) {

               result[j++] = kOld;
           } else {
               int newValue;
               boolean flag1 = false;
               do {
                   System.out.println("Lottery requires " + k
                           + " distinct numbers.");
                   System.out
                           .println("Please select a replacement lottery number for "
                                   + kOld);

                   boolean flag = false;
                   do {
                       newValue = scanner.nextInt();
                       if (newValue > n) {
                           System.out
                                   .println("Lotto number is outside range.");
                           System.out.println("Try again: ");

                       } else if (newValue <= 0) {
                           System.out.println("Lotto number less than 1? Eh.");
                           System.out.println("Try again: ");

                       } else {
                           flag = true;

                       }
                   } while (flag == false);
                   if (!isExists(result, newValue)) {

                       result[j++] = newValue;
                       flag1 = true;
                   } else {
                       flag1 = false;
                   }
               } while (flag1 == false);

           }
       }
       return result;
   }

   private static boolean isExists(int[] array, int value) {
       for (int i : array) {

           if (i == value) {

               return true;
           }
       }
       return false;
   }

}

OUTPUT:

Enter n range of non-Powerball numbers 1 to n:
7

Enter number of k distinct integers:
5

Pick your lottery numbers:
1
2
2
3
4
Lottery requires 5 distinct numbers.
Please select a replacement lottery number for 2
1
Lottery requires 5 distinct numbers.
Please select a replacement lottery number for 2
-2
Lotto number less than 1? Eh.
Try again:
9
Lotto number is outside range.
Try again:
5

5

Pick your powerball number:
6

Lottery numbers selected:
1, 2, 5, 3, 4

Powerball number selected:
6

Enter 1 to quit or any other key to play again:
1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote