0

I am doing a Lotto application in a jForm/GUI in Netbeans with 3 rows of 5 numbers, and I don't want duplicates to be allowed on each line. To have one number on line 1 and the same on line 3 is OK, but to have those numbers on the same line is not OK.

The only way I can think of doing it that I know will work is to hard code it, and preferably, I don't want that.

I have tried:

    boolean dup = false;
    for (int k = 0; k < num[0].length){ //loop through columns
     for (i = 0; i < num.length-1; i++) {
        for (int j = i; j < inArray.length; j++){
          if (num[k][i] == num[k][j]){
            dup = true;
            break;
          }
        }
      }
    } 

and this:

    public static boolean hasDuplicates(int [][] num) {
        for (int row = 0; row < num.length; row++) {
            int curRow = num[row];
           Set set = Sets.newHashSet(Arrays.asList(curRow));
            if (set.size() < curRow.length) {
               return true;
           }
       }
        return false;
   }

I have also looked at other coding extensively and I can't get one that works.

The exact thing I'm trying to do is:

Get user's input for three lines of Lotto via text field, check each line for duplicates, print to a jLabel if it's a duplicate or leave the jLabel blank and run the rest of the code if there's no duplicates.

The current code I have is:

        private void playBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        

    num[0][0] = Integer.parseInt(line00Tf.getText());
    num[0][1] = Integer.parseInt(line01Tf.getText());
    num[0][2] = Integer.parseInt(line02Tf.getText());
    num[0][3] = Integer.parseInt(line03Tf.getText());
    num[0][4] = Integer.parseInt(line04Tf.getText());
    num[1][0] = Integer.parseInt(line10Tf.getText());
    num[1][1] = Integer.parseInt(line11Tf.getText());
    num[1][2] = Integer.parseInt(line12Tf.getText());
    num[1][3] = Integer.parseInt(line13Tf.getText());
    num[1][4] = Integer.parseInt(line14Tf.getText());
    num[2][0] = Integer.parseInt(line20Tf.getText());
    num[2][1] = Integer.parseInt(line21Tf.getText());
    num[2][2] = Integer.parseInt(line22Tf.getText());
    num[2][3] = Integer.parseInt(line23Tf.getText());
    num[2][4] = Integer.parseInt(line24Tf.getText());

        duplicateLbl.setText("");
        LottoPhase1 p1 = new LottoPhase1();
        p1.setNum(num);
        p1.createSecret();
        secret = p1.getSecret();
        p1.computeCheckInput();
        correctL1 = p1.getCorrectL1();
        correctL2 = p1.getCorrectL2();
        correctL3 = p1.getCorrectL3();

        //prints secret to output
        System.out.println("Phase 1 Main Secret: " + Arrays.toString(secret));
        System.out.println();


        displayResults0Lbl.setText(Integer.toString(secret[0]) + ", " + Integer.toString(secret[1]) + ", " + Integer.toString(secret[2]) + ", " + Integer.toString(secret[3]) + ", " + Integer.toString(secret[4]));

        matched1NumLbl.setText(Integer.toString(correctL1));
        matched2NumLbl.setText(Integer.toString(correctL2));
        matched3NumLbl.setText(Integer.toString(correctL3));
    }

2 Answers2

0

The second method has a couple of errors, for instance,

int curRow = num[row];

Should actually be:

int[] curRow = num[row];

Also, you appear to be using Sets, which probably comes from some library you're using (Guava, Google Common, etc.). Assuming you were not using any library, you could change your code to something similar to:

public static boolean hasDuplicates(int [][] num) {
    for (int[] curRow : num) {
        Set<Integer> set = new HashSet<>();
        for (int n : curRow) {
            if (!set.add(n)) {
                return true;
            }
        }
    }
    return false;
}

If you're using Java 8, one way to remove the second for loop is by using a Stream:

public static boolean hasDuplicates(int [][] num) {
    for (int[] curRow : num) {
        Set<Integer> set = IntStream.of(curRow).boxed().collect(Collectors.toSet());
        if (set.size() < curRow.length) {
            return true;
        }
    }
    return false;
}

Other alternatives to the Stream can be found in threads like these. Testing with the following input produces what I think you would expect:

int[][] testA = {{0,1,2,3,4}, {0,1,2,3,4}, {0,1,2,3,4}}; //false
int[][] testB = {{0,1,2,3,4}, {0,2,2,3,4}, {0,1,2,3,4}}; //true
int[][] testC = {{0,1,2,3,4}, {0,1,2,3,4}, {0,4,3,3,4}}; //true
int[][] testD = {{0,1,2,3,4}, {5,6,7,8,9}, {10,11,12,13,14}}; //false
Community
  • 1
  • 1
Fabio Alves
  • 3
  • 1
  • 3
0
public static boolean hasDuplicates(int[][] num)
{
    boolean hasDuplicate  = false;
    // for each line in num
    for(int[] line : num)
    {
        // for every number in the row
        for(int i = 0; i < line.length && !hasDuplicate; i++)
        {
            // for every number in the row
            for(int j = 0; j < line.length; j++)
            {
                // if we are not comparing the same number
                if(i != j)
                {
                    // check for equality
                    if(line[i] == line[j])
                    {
                        hasDuplicate = true; // we have found a duplicate
                        break; // no need to keep checking; break the loop and return
                    }
                }
            }
        }
    }
    return hasDuplicate;
}
David
  • 361
  • 2
  • 10
  • Worked perfectly, thank you. I had tried the others and they were not working, only this one worked. Thanks, much appreciated. –  Nov 28 '16 at 11:03