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

I\'m trying toassign five numbers to an array without duplicating a number. Thec

ID: 3618549 • Letter: I

Question

I'm trying toassign five numbers to an array without duplicating a number. Thecode I have doesn't work for some reason. I know why it is out ofbounds, but it is still assigning duplicatenumbers sometimes.  

private static int[] numbersArray = {1, 2, 3, 4, 5, 6, 7, 8, 9,0};
while (programNumArray[i] != randomNum)//;do
  {
   for (i = 0; i <= 5; i++)
   {
    randomNum =generator.nextInt(numbersArray.length);
    programNumArray[i] = randomNum;
    System.out.println(programNumArray[i]);
   }
  } I'm trying toassign five numbers to an array without duplicating a number. Thecode I have doesn't work for some reason. I know why it is out ofbounds, but it is still assigning duplicatenumbers sometimes.  

private static int[] numbersArray = {1, 2, 3, 4, 5, 6, 7, 8, 9,0};
while (programNumArray[i] != randomNum)//;do
  {
   for (i = 0; i <= 5; i++)
   {
    randomNum =generator.nextInt(numbersArray.length);
    programNumArray[i] = randomNum;
    System.out.println(programNumArray[i]);
   }
  }

Explanation / Answer

As far as I can tell, your program either: 1) Won't run at all: this will occur if randomNumber andprogramNumArray[i] are equivalent when you reach the whileloop. 2) Will run forever. The latter will occur because your program is set to stop whenprogramNumArray[i] is not equal torandomNum. Here are your problems: 1) Except for the first iteration, your loop will always comparerandomNum to programNumArray[6], theseventh element of the (zero-indexed)programNumArray. Your for loop is exited when i