JScannell
JScannell

Reputation: 13

Read int array and check if it's sorted

I have an int array and I am trying to figure out how I can make the program so that it stores the integers entered by the user by assigning the values to elements of an array of ints. I am trying to place the code so it accepts the integers from the user in a loop (a while or for loop) that executes 10 times. And then have code that checks the sequence of integers to see that it is non-decreasing in another loop that executes 9 times using two separate loops. Below is what I have to form this "Checker" from.

class ArrayRecorder {
    public static void main(String[] args) {
        int Array[] = new int[10];
        System.out.println("Enter ten integers seperated by spaces, then press return after. ");    
        for(int count = 0; count < 10; count++)
            Array[count] = A3Helper.nextInteger();
        for (int count = 0; count < 10; count++ )
            System.out.print(Array[count]+ " ");

Sorry if this seems like a stupid question, I think I am overthinking it. I have tried to modify the code, but cannot figure it out, below is what I have started.

class ArrayChecker {
public static void main(String[] args) {
    int Array[] = new int[10];
    System.out.println("Enter ten integers seperated by spaces, then press return after. ");    
    for(int count = 0; count < 10; count++)
        Array[count] = A3Helper.nextInteger();
    System.out.print("List is in order");

Here is an example of what the input and output from your program should look like: Enter ten integers separated by spaces, then press return. 10 3 0 -4 200 80 -95 87 23 -67 List is not in order.

Here is another example: Enter ten integers separated by spaces, then press return. -5 -4 -3 0 1 2 2 3 10 11 List is in order

also A3Helper code:

import java.util.Scanner;
class A3Helper {
static Scanner keyboard = new Scanner(System.in);
static int nextInteger() {
    return keyboard.nextInt();
}

}

Upvotes: 0

Views: 201

Answers (2)

Max Vollmer
Max Vollmer

Reputation: 8598

You're almost there. What you're missing is doing an actual compare inside your loop and storing the result somewhere:

boolean listIsInOrder = true;
for (int i = 1; i < 10; i++) {
    if (array[i] < array[i-1]) {
        listIsInOrder = false;
    }
}

Here we use a boolean listIsInOrder to store if the list is in order or not.

The loop starts at index 1 instead of 0, because inside the loop we always compare each value at index i with the value before it, which is at index i-1.

As soon as the loop finds a value that is smaller than the value that came before it, it sets listIsInOrder to false. If all values in the array are in order, listIsInOrder will not be changed and remain true.

You can then simple check the value of listIsInOrder:

if (listIsInOrder) {
    // handle valid input
}
else {
    // handle invalid input
}

Please note that I have renamed your array, because variable names in Java should start with a lower case letter. Capital letters are for class names. Sticking to such conventions makes your code much more readable.

Upvotes: 0

Nick Silvestri
Nick Silvestri

Reputation: 321

Just check that each successive number is greater than the one before it. If not, you can just change a boolean that indicates whether the array is in order and print out the different strings.

boolean inOrder = true;
for (int count = 0; count < 9; count++) {
    if (Array[count + 1] < Array[count]) {
        inOrder = false;
        break;
    }
}

if (inOrder) {
    System.out.println("Array is in order.");
} else {
    System.out.println("Array is not in order.");
}

Note that we're only looping through array indices 0 through 8, and not 9, because otherwise Array[count + 1] would result in an ArrayIndexOutOfBoundsException.

Upvotes: 1

Related Questions