I'm doing the old *Lotto* exercise and I need to specifically use an **Array[]** of integers and not an **ArrayList**. I have what I thought would work, but I seem to be wrong. I looked for posts similar to these and all of them involved an **ArrayList<>**. Here is a partition of my code.

```
Integer[] lottoNums;
lottoNums = new Integer[7];

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

    lottoNums[i] = randomNums.nextInt((59)+1);

    if(i <= 5) {
        if(lottoNums[i].equals(lottoNums[i+1])){
            if(lottoNums[i] < 58 && lottoNums[i] > 1)
                lottoNums[i] = lottoNums[i] +1;
        }
    }
    else if(i >= 1) {
        if(lottoNums[i].equals(lottoNums[i-1])){
            if(lottoNums[i] < 58 && lottoNums[i] > 1)
                lottoNums[i] = lottoNums[i] +1;
        }
    }
}

Arrays.sort(lottoNums);
System.out.print("Winning numbers: "+lottoNums[0]);
for (int i = 1; i < 6; i++) { 
     System.out.print(", " + lottoNums[i]);
}
System.out.print(System.getProperty("line.separator"));
System.out.println("Bonus Number: "+lottoNums[6]);

```

I need to get it to generate a number in between `1` and `59` and not duplicate. I was trying to pair it up with the value stored in the element before and after it (if it had one) and if it was equal to it, it would add 1 to it. I run it a few times and every once in a while im still getting duplicate numbers. How can i do this efficiently, using **Arrays[] of integers ONLY?**

EDIT:

Initialized array to remove NullPointerException.

Updated Code:
=============

---

---

```
for(int i = 0; i < lottoNums.length; i++){

        lottoNums[i] = randomNums.nextInt((59)+1);

    }
for (int i = 0; i < 6; i++) {
           int rnd = randomNums.nextInt((lottoNums).length-i);
           int k = lottoNums[lottoNums.length-i-1];
           lottoNums[lottoNums.length-i-1] = lottoNums[rnd];
           lottoNums[rnd] = k;
        }

    Arrays.sort(lottoNums);
    System.out.print("Winning numbers: "+lottoNums[0]);
    //PRINTING LOTTO NUMBERS
    for (int i = 1; i < 6; i++) { 
         System.out.print(", " + lottoNums[i]);
    }
        System.out.print(System.getProperty("line.separator"));
        System.out.println("Bonus Number: "+lottoNums[6]);

```