Question

I am trying for too long to figure out this exersies but I am stuck here. I need to write a boolean method that will an array as argument and should return true if numbers in array are in decreasing order. Bu any time that I am trying I am having the same value or errors. Here is my code:

public class Question1c{
    public static void main (String[] args){
        int[] arr = {1, 9, 3, 4, 5, 6};
        boolean product = isDecreasing(arr);
        System.out.println(product);
    }

    public static boolean isDecreasing (int[] numbers){
        int first = numbers[0];

        for (int i : numbers){

            if(first <= i){
                first = i;
                return true;
            }

            //else{
            //  return false;
            //}
        }return false;
    }
}
Was it helpful?

Solution

The problem in your code is that you cannot return true until after you went through the entire array. However, you can return false as soon as you detect an "inversion" - i.e. a situation when the number that follows the one you've seen before is greater than the prior number.

You are reasonably close to a working solution - you need to remove return true, uncomment the else, and change the final return false to return true.

To make your code more readable, rename first to prior. Also consider changing the "foreach" version of the for loop to a regular for loop that skips the initial element of the array. This would let you detect decreasing order, as opposed to non-increasing, which you currently detect.

OTHER TIPS

Not sure I understand, but wouldn't this solve your issue?

public class Question1c{
    public static void main (String[] args){
        int[] arr = {1, 9, 3, 4, 5, 6};
        boolean product = isDecreasing(arr);
        System.out.println(product);
    }

    public static boolean isDecreasing (int[] numbers){
        for (int i = 0; i < numbers.Length; i++){
            if (i == 0)
                continue;
            if (numbers[i - 1] >= numbers[i])
                return false;
        }
        return true;
    }
}

You're essentially just aiming to check that the previous item in the array isn't greater than or equal to the current item in the array, aren't you?

You initialize first with numbers[0] this causes several problems:

  • An empty array throws IndexOutOfBoundsException
  • the first check automatically passes (numbers[0] <= numbers[0])

you best check the length of numbers for 0 and use a "normal" for loop (using an index).

Your return values is also the negation of what it should be.

This might solve your issue.

public class Question{
public static void main (String[] args){
    int[] arr = {1, 9, 3, 4, 5, 6};
    boolean product = isDecreasing(arr);
    System.out.println(product);

}

public static boolean isDecreasing (int[] numbers){
    int first = numbers[0];
    for (int i = 1; i < numbers.length ; i++) {

        if (first <= numbers[i]) {
            return false;
        } 
        first = numbers[i];
    }
    return true;
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top