Validate variable is in range Java

java

Solution

The only improvement I think could be made (for readability) is this:

public boolean isInRange(int i) {
    return i > 0 && i < 6;
}

Then call that function instead.

If `a`, `b`, and `c` are related, you may also consider using an array and looping through that. Something like:

public boolean allInRange(int[] arr) {
    for (int i = 0; i < arr.length; i ++) {
        if (!isInRange(arr[i])) return false;
    }
    return true;
}

Problem

I have three integers, which are assigned to variables after being passed in as command line parameters. I want to validate that each of the integers are in the range 1-5. Is there a way to accomplish this in Java without using an if statement like the one below? I'd like to avoid doing this (note- pseudocode): ``` if ((a & b & c) > 0 && (a & b & c) < 6) { //blah blah } ``` Mainly, this wouldn't scale well if additional parameters were added in the future, etc. Is there a more elegant way to accomplish this?

Original source