user3420212
user3420212

Reputation:

how to stop a boolean return from breaking a loop?

I'm trying to write code that checks if an array is sorted, homework says it must be in a method with a boolean return, but if I put the return inside of the for loop it stops the for loop before it can check more than just the first two digits in the list. And either way, even if I put the returns within the loop eclipse tells me that there is no boolean return in the method? I'm not sure how to get it to work and can't find anything similar online please help :)

public static boolean isSorted (double [] list) {
    for (int i=1; i<list.length; i++) {
        if (list[(int) i-1] > list[(int) i] ) {
            System.out.println("Not Sorted");
            return false;
        }
        else {
            return true;
        }
    }
}

Upvotes: 0

Views: 1415

Answers (7)

user2486310
user2486310

Reputation: 11

The main issue here is: the compiler cannot check whether the for-loop is entered. If the list is empty, the function will indeed return nothing! And therefore is in conflict with the promise to return a boolean. That is why the proposed answers above work: they always return a boolean if the for-loop finished (or did not run at all).

Upvotes: 0

SBI
SBI

Reputation: 2322

This is just for providing an alternate answer to the ones discussed here. I see the benefit in learning the iterative version first, but the logic can be abstracted to something like this:

public static boolean isSorted(double[] list) {
    return Arrays.equals(list, Arrays.sort(list));
}

Upvotes: 3

Vitkinov
Vitkinov

Reputation: 79

Try this

public static boolean isSorted (double [] list){

    boolean result = true;

    for(int i=1; i<list.length; i++) {
        if (list[i-1] > list[i]) {
            System.out.println("Not Sorted");
            result = false;
            break;
        }
    }

    return result;

}

Upvotes: 2

Lucian Novac
Lucian Novac

Reputation: 1265

public static boolean isSorted (double [] list){
    boolean isSorted=true;
    for(int i=1; i<list.length; i++){
        if (list[i] > list[i+1]){
        System.out.println("Not Sorted");
        isSorted = false;
    }
    }
    return isSorted;
}

Upvotes: -2

Mischa
Mischa

Reputation: 99

Is this a school project? Then I don't think your teacher likes to see you use a for-loop if you break it at a certain point. Use a while loop instead.

public static boolean isSorted (double [] list) {
    boolean sorted = true;
    int i = 1;
    while (i < list.length && sorted) { 
        sorted = (list[i-1] > list[i]);
        i++;
    }
    return sorted;  
}

Not tested though, you might have to fine-tune the i.

Upvotes: 0

ruhungry
ruhungry

Reputation: 4706

public static boolean isSorted(double[] list) {

    for (int i = 1; i < list.length; i++) {
        if (list[i - 1] > list[i]) {
            System.out.println("Not Sorted");
            return false;
        }
    }
    return true;
}

Try this code. You will return true in the end if it is sorted

Upvotes: 6

nikis
nikis

Reputation: 11234

Just traverse array and if you find that list[i] > list[i+1], return false. It can be done at the following way:

public static boolean isSorted (double [] list){

    for(int i=0; i<list.length-1; i++){
        if (list[i] > list[i+1]){
            System.out.println("Not Sorted");
            return false;
        }
    }

    return true;
}

Upvotes: 2

Related Questions