0

I am new to coding and I am wondering how I can make sure a specific array contains certain values?

This is what I have but it is not working for me. I have tried searing the internet for a solution but I am confused by a lot of them. Any feedback helps!

public static boolean allnumbers(int[] arr) {// makes sure only numbers 1-9 are used
    boolean[] found = new boolean[9]; // creates a boolean array 

    for (int row = 0; row > 1 && row > 9; row++) {
        int index = arr[row] - 1;
        if(!found[index]) {
            found[index] = true;
        } else{
            return false; //returns false if there are numbers that are not between 1-9
        }
    }
    return true;
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show *specifically* what you've found and state *specifically* how it confuses you. Try to include information that helps clarify and specify your question. – Hovercraft Full Of Eels Nov 22 '18 at 03:32
  • 2
    your for loop is wrong. try `for(int row=0; row<9; row++)` – omurbek Nov 22 '18 at 03:34
  • @HovercraftFullOfEels It's tagged with sudoku. – shmosel Nov 22 '18 at 03:36
  • 1
    Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints. – Hovercraft Full Of Eels Nov 22 '18 at 03:37
  • See also [Iterating through a variable length array](https://stackoverflow.com/questions/2331509/iterating-through-a-variable-length-java-array). – user1803551 Nov 22 '18 at 03:38
  • This duplicates with [https://stackoverflow.com/questions/1128723/how-to-determine-whether-an-array-contains-a-particular-value-in-java](https://stackoverflow.com/questions/1128723/how-to-determine-whether-an-array-contains-a-particular-value-in-java) – Siddhivinayak Shanbhag Nov 22 '18 at 03:41

2 Answers2

0
/**
 * Checks if this array consists of numbers 1 to 9, with all unique numbers,
 * and no number missing.
 *
 * @param arr input array
 * @return true if this array has numbers 1 to 9, each occurring exactly once
 */
public static boolean allnumbers(int[] arr) {
    if (arr.length != 9) return false;
    return IntStream.rangeClosed(1, 9)
            .noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
}

Another way...

public static boolean allnumbers(int[] arr) {
    return Arrays.stream(arr).boxed().collect(Collectors.toSet())
            .equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
}

Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-

public static boolean allnumbers(int[] arr) {
    return Arrays.stream(arr)
            .noneMatch(i -> i < 1 || i > 9);
}
Kartik
  • 7,677
  • 4
  • 28
  • 50
-2

This is a solution for your problem:

public static boolean allnumbers(int[] arr) {// makes sure only numbers 1-9 are used
    for(int row=0; row < 9; row++){
        if (arr[row] < 1 || arr[row] > 9)
            return false;
    }
    return true;
}

this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.

PhaseRush
  • 340
  • 4
  • 10