Skip to content
Advertisement

Ordered or unordered Sequence problem in java

I am facing problem in solving the question below:- (can someone please help????)

Write a program that reads a sequence of integer numbers and outputs true if the sequence is ordered (in ascending or descending order), otherwise, false. Keep in mind, if a number has the same value as the following number, it does not break the order. The sequence ends with 0. Do not consider this number as a part of the sequence. The sequence always has at least one number (excluding 0).

code which I wrote(For this code I am getting runtime error)…..

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        int number1 = scanner.nextInt();
        int number2;
        int number3;
        int number4;
        int prev;
        int mid;
        int next;
        int flag = 1;
        boolean val = true;
        while (val) {
            if (number1 > 0) {
                 number2 = scanner.nextInt();
                if (number2 > 0) {
                     number3 = scanner.nextInt();
                    if(number3 > 0){
                        prev = number1;
                         mid = number2;
                         next = number3;
                        if (prev >= mid && mid >= next || prev <= mid && mid <= next) {
                            number4 = scanner.nextInt();
                            prev = number2;
                            mid = number3;
                            next = number4;
                            flag = 1;
                        } else {
                            break;
                        }
                    } else {
                        if (number1 >= number2 && number2 >= number3 || number1 <= number2 && number2 <= number3) {
                            flag = 1;
                        } else {
                            flag = 0;
                        }
                    }
                } else {
                    if (number1 >= number2 || number1 <= number2) {
                        flag = 1;
                    } else {
                        flag = 0;
                    }
                }
            } else {
                flag = 0;
            }

        }
        if (flag == 1) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }

    }
}```

Advertisement

Answer

Your code was a little bit messy, I will post a cleaned up version and explain the single steps.

class Main {
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    //firstable, you do not need to declare seperate variables for the number 
    //you want to enter, but you can use those (here currentNumber is the 
    //number typed in most recently, midNumber is the number before that, 
    //previousNumber is the one before that); they are initialized with 0 for 
    //reasons you will they later
    int currentNumber = 0, midNumber = 0, previousNumber = 0;
    boolean flag = false;

    //a while loop, which asks for new numbers, as long as the user does not 
    //break it
    while(true){
        //the user enters a number, which is the new current number
        System.out.print("Enter a number: ");
        currentNumber = scanner.nextInt();
        //if the entered number is 0 and there was one number entered before,
        //the while loop breaks and the process ends
        if(currentNumber == 0 && midNumber != 0){
            break;
        }
        //there have to be at least three numbers in the sequence for it to be 
        //tested, otherwise the sequence is always true, so there have to be a 
        //midNumber and a previousNumber
        if(previousNumber != 0 && midNumber != 0){
            flag = false;
            //the flag is only true, if the three entered numbers are either 
            //descending or ascending
            if(
                (previousNumber <= midNumber && midNumber <= currentNumber)
                ||
                (previousNumber >= midNumber && midNumber >= currentNumber)
            ){
                flag = true;
            }
        }
        //the previous number is set to the midNumber, the midNumber is set to 
        //the currentNumber
        previousNumber = midNumber;
        midNumber = currentNumber;
       //since the process has not been terminated by the user, the while-loop 
       //starts over
    }
    // for memory reasons, the scanner has to be closed, since it will not be 
    //used after the while-loop, it will be closed here
    scanner.close();

    //the flag is printed; true if the sequence is true, false if otherwise
    System.out.println(flag);
}

}

Advertisement