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!!! :(sweat):

Printable View

- 02-08-2012, 09:36 PMcarlasofbValidate 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!!! :(sweat): - 02-08-2012, 10:11 PMFubarableRe: Validate an array????
Let's see what you've tried so far.

- 02-08-2012, 10:23 PMcarlasofbRe: 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;

} - 02-10-2012, 01:07 AMstchmanRe: 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

... - 02-13-2012, 03:13 PMcarlasofbRe: 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! :(happy):

- 02-13-2012, 03:34 PMJosAHRe: 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