1. Member
Join Date
Feb 2012
Posts
3
Rep Power
0

## Validate an array????

Hi, I'm starting to learn JAVA and they asked me for a method that checks if an array of 9 positions contains the numbers 1-9 (the order doesn't matter).

Plz help!!!

2. Member
Join Date
Feb 2012
Posts
3
Rep Power
0

## Re: Validate an array????

public static boolean contains1to9(int[] v) {

boolean check = true;
int count = 0;

for (int j=0; j<v.length-1; j++) {
if (v[j] == v[j+1]) {
count++;
check = false;
}

if (count == 0 && v[j] > 0 && v[j] < 10)
check = true;
}

return check;

}

3. ## Re: Validate an array????

Let me get this straight, the array must be of size 9 and contain the integers 1-9 in any order? ex:

v = { 1,2,3,4,5,6,7,8,9 } or
v = { 1,9,2,8,3,7,4,6,5 } or
v = { 9,1,6,7,8,3,2,5,4 } or
...

4. Member
Join Date
Feb 2012
Posts
3
Rep Power
0

## Re: Validate an array????

Yes, that's it. I think my code works now, I've done a few changes to it. It may not be the best but it does what's supposed to!

5. ## Re: Validate an array????

Stick them all in a SortedSet and afterwards check if the first (lowest) element is a 1, the last (highest) element is a 9 and that the Set contains nine elements.

kind regards,

Jos