# Thread: Error if array contains duplicate integers

1. Member Join Date
Nov 2009
Posts
23
Rep Power
0

## Error if array contains duplicate integers

I posted earlier about an array having random integers but one integer cannot appear twice. I got some feedback and was finally able to figure it out, but now I'm stuck at another place.

Previous topic: http://www.java-forums.org/new-java/...er-values.html

Basically, the whole concept is to have a computer generate a random number of digits between 2 and 10, with each digit in the number NOT appearing twice.

Then, the user must enter a number from his keyboard to guess the number, knowing the number of digits in the randomly generated number. I'm trying to accomplish this part but it's not happening.

What I did was ask for a number from the user, then pass it into a .hasNext() and then verify the input to make sure it contains numbers, is of the right size, BUT I'm stuck at finding out whether the number contains any duplicate values.

Here is my code
Java Code:
```//
// File: Master.java               Compiler:       Scite
//

import java.util.Scanner;

public class Master
{
final static int MAX = 10 - 1; // the maximum digits in a randomly generated number
final static int MIN = 2; // the minimum digits in a randomly generated number
final static char YES = 'Y'; // to continue or keep going
final static char NO = 'N'; // to stop

public static void main (String[] args)
{
int size = randomSize(); // call a function to randomly generate an integer based on constant restrictions
int[] computerArray = createArray(size);
printArray(computerArray); // call functions to create an array and print it

System.out.println("The number consists of " + size + " values.\n");
printArray(userArray);
}

{
Scanner input = new Scanner(System.in); // start keyboard Scanner
int guess = 0; // initialize the guess to 0
int digits = 0; // initialize the number of digits in the guess to zero
int[] userArray = new int[computerArray.length]; // make an array equal to the randomly generated computer array

while (input.hasNext()) // create a loop to keep asking for a value until the right value has been entered
{
if (input.hasNextInt()) // if the user entered an integer
{
guess = input.nextInt(); // set guess to this integer value
digits = countDigits(guess); // count the number of digits in this guess to see if it equals the array
if (digits > computerArray.length || digits < computerArray.length) // if it's less or more
{
System.out.println("\nThe value you entered is not of the correct range"); // show the error
System.out.print("Enter a guess: "); // let them try again
}

else // if it is equal to the array
{
userArray = createUserArray(digits, guess); // create an array of the value entered
boolean check = checkSimilarDigits(userArray); // check if this array has any duplicate values
if (check == true) // if it does
{
System.out.println("The value you entered has duplicate digits!"); // show the error
System.out.println("Please use a digit only once!"); // tell them how to fix it
System.out.print("Enter a guess: "); // let them try again
}
}
}

else
{
input.next(); // if user input is anything other than an integer
System.out.println("\nThat is not a valid value!"); // show the error
// tell them what the correct range should be
System.out.println("Please guess an INTEGER value that consists of " + computerArray.length + " digits");
System.out.print("Try again: "); // let them try again
}

}

return userArray;
}

private static int countDigits(int number)
{
int count = 0; // initilaize counter to zero
if (number < 0) // check to see if the number is positive
number *= -1; // if not, turn it into a positive value

while (number > 0) // begin loop to start counting digits
{
number /= 10; // keep chopping off a digit
count++;
}

return count;
}

private static int randomSize() // function to generate a random integer
{
return MIN + (int)(Math.random() * MAX); // return an integer between those constant values
} // function end

private static int[] createArray(int size) // function to create an array based on a random size generated in the "randomSize" function
{
int[] array = new int[size]; // create the new array of the size of that integer variable
int count = 0; // initialize a timer
for (int i = 0; i < array.length; i++) // start adding values to the array
{
if(i == 0) // for the first element in the array
{
array[i] = randomInteger(); // call a function to assign this a random integer
count++; // increase the counter by 1
}

else // for all other elemebts in the array
{
int randomvalue = randomInteger(); // call a function to generate a random integer and assign it to this variable
boolean doesexist = search(array, randomvalue, count); // do a check to see if the variable equals any other values in the array
while (doesexist != true) // while the variable value does exist
{
randomvalue = randomInteger(); // keep generating random values and assigning it to the variable
doesexist = search(array, randomvalue, count); // until you find one that hasn't already been assigned
}

array[i] = randomvalue; // assign the next element in the array that randomly generated value
count++; // and increase the counter by 1;
}
}

return array; // return this array
} // function end

private static boolean search(int array[], int value, int count) // function to search for similar integer values
{
for (int i = 0; i < count; i++) // start a loop that goes uptil the array size that has already been assigned values
{
if (value == array[i]) // if the randomly generated variable equals any array element
return false; // return a false
}

return true; // otherwise return a true
} // funtion end

private static int randomInteger() // function to get a random integer
{
return (int)(Math.random() * 10); // return a random integer from 0 to 9
} // function end

private static int[] createUserArray(int size, int number) // function to create an array based on a random size generated in the "randomSize" function
{
int[] array = new int[size]; // create the new array of the size of that integer variable
for (int i = array.length - 1; i >= 0; i--) // start adding values to the array
{
array[i] = (number % 10);
number /= 10;
}

return array; // return this array
} // function end

private static boolean checkSimilarDigits(int[] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 1; j < array.length - 1; j++)
{
if (array[i] == array[j])
return true;
}
}

return false;
}

private static void printArray(int[] array) // function to print an integer array
{
for (int i = 0; i < array.length; i++) // start loop to start printing
{
System.out.print(array[i]); // print all values one after the other
}
System.out.println();
} // function end

}```
I first see that the digits in the number entered from the keyboard is of the correct value. If it is, then make a userArray of this number. Then, check each digit in the array against the other in the function "checkSimilarDigits" and return a boolean.

If it is true, show an error and go back into the loop, otherwise return.

However, it doesn't work. The boolean keeps returning whatever I assign it and doesn't do it's job. So no matter which number I chooce, with similar or non-similar digits, I still get the error.

Can anyone please tell me what I'm doing wrong here?  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Java Code:
```private static boolean checkSimilarDigits(int[] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 1; j < array.length - 1; j++)
{
if (array[i] == array[j])
return true;
}
}

return false;
}```

Consider what happens the second time around the outer loop when i is one. j will also be one initially so "array[i] == array[j]" will be true and this method will (almost always) return true. Is this what you want?  Reply With Quote

3. Member Join Date
Nov 2009
Posts
23
Rep Power
0

## I see what you're saying! I finally see it! LOL

No, I want to find duplicate values within the array. How would I do that? I know that i and 1 will always equal j at one which is why this is happening.

How can I do this without that happening?  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## The easiest way would be to check i and j. If they are equal don't bother checking array[i] and array[j].  Reply With Quote

5. gcampton Guest

## final static int MAX = 10 - 1; // the maximum digits in a randomly generated number

why not just say 9 ? it is a constant  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•