Tdag
Tdag

Reputation: 23

Checking For Repeats

I'm trying to write code that checks for repeat numbers in an array. If numbers repeat within the same row, it should generate another random number to replace the number. This is my class/method that is meant to accomplish this goal:

import java.util.*;

public class NoRepeats
{
    public static void clean(int ticks[][])
    {        
        for(int i = 0; i < ticks.length; i++)
        {
            for(int j = 0; j < ticks[0].length; j++)
            {
                nums[j] = ticks[i][j];   
            }
            for(int k = 0; k < nums.length; k++)
            {
                for(int count = k + 1; count < nums.length; count++)
                {
                    int othNums = nums[count];
                    while(true)
                    {
                        if(nums[k] == othNums)
                        {
                            System.out.print("\n\n Repeat:" + nums[k] + k + "  " + othNums + "\tTicket Number: " + (i+1) +"\n\n\n");
                            nums[k] = 1 + rndm.nextInt(48);
                            for(int counter = count; counter < nums.length; counter++)
                            {
                                int othNums2 = nums[count];
                                if(nums[k] == othNums2)
                                {
                                    System.out.print("\n\n Repeat NUM2:" + nums[k] + "  " + othNums2 + "\tTicket Number: " + (i+1) +"\n\n\n");
                                    nums[k] = 1 + rndm.nextInt(48);
                                }
                            }
                            for(int l = 0; l < k; l++)
                            {
                                int othNums3  = nums[l];
                                if(nums[k] == othNums3)
                                {
                                    System.out.print("\n\n RepeatNUM2: " + nums[k] + "  " + othNums3 + "\tTicket Number: " + (i+1) +"\n\n\n");
                                    nums[k] = 1 + rndm.nextInt(48);
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    count++;
               }   
            }
            for(int q = 0; q < nums.length; q++)
            {
                ticks[i][q] = nums[q];
            }
            count = 0;
        }
    }
    public static int nums[] = new int[6];
    public static int count = 1;
    public static Random rndm = new Random();
    public static int numsMatched[] = new int[100];
}

For some reason, there are still matches and I don't know why. Can anyone find what I've done wrong?

Upvotes: 0

Views: 50

Answers (1)

Thomas
Thomas

Reputation: 88707

You're incrementing count twice and thus skip entries:

for(int count = k + 1; count < nums.length; count++) //<- increment 1
{
  int othNums = nums[count];
  while(true)
  {
    //snip
  }
  count++; //<- increment 2
}

The problem probably originates from the fact that you have a static variable which is also called count and which will be shadowed by the count defined in the loop:

public static int count = 1;

So either remove the second increment or rename one of the variables.

Upvotes: 1

Related Questions