1

I am given a list of 3 digit numbers and I am trying to see if they are in descending order. The number of elements in the list is not determined but I have set my SENTINEL value to be 1000.

The following error keeps happening though:

CompileRunTest: throwable = java.util.NoSuchElementException
java.util.NoSuchElementException

My code:

import java.util.Scanner ;

public class StrictDescending {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        final int SENTINEL = 1000 ;
        int firstValue = in.nextInt();
        while (firstValue < 1000)
        {
            int secondValue = in.nextInt() ;
            while(secondValue < 1000)
            {
                if(firstValue > secondValue)
                {
                    System.out.println("Yes, the list is in descending order.") ;
                }
                else
                {
                    System.out.println("No, the list is not in descending order.") ;
                }

                secondValue = in.nextInt();
            }

            firstValue = in.nextInt() ;
        }
    }

}
8
  • your code is ok try again Commented Jun 9, 2016 at 17:15
  • That error gets throw if there's no more input to consume. How are you testing it? Commented Jun 9, 2016 at 17:17
  • 145 130 125 100 1000 and 145 130 130 125 1000 those are two different given inputs Commented Jun 9, 2016 at 17:17
  • Why declare the SENTINEL = 1000; if you aren't using it anywhere? Commented Jun 9, 2016 at 17:40
  • 1
    Do not vandalize your post. Commented Jun 9, 2016 at 20:57

2 Answers 2

1

Try switching the first while to an if statement, add firstValue = secondValue on the line before secondValue = in.nextInt(); and remove the last firstValue = in.nextInt(); You'll need to mess around with your print statement a little as well.

As it is your program flow doesn't quite make sense since you will try to consume from stdin even when there are no numbers left.

Sign up to request clarification or add additional context in comments.

2 Comments

okay i did that for the inputs 145 130 125 100 1000 and it gave me Yes, the list is in descending order (3 times repeated)
I'll let you figure that part out. I can't do all your homework for you ;)
0

EDITED ANSWER

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int previousValue = getInt(sc);
    while(Math.abs(previousValue) < 1000) {
        if (previousValue <= (previousValue = getInt(sc))) {
            System.out.println("No, the list is not in descending order.");
            break;
        }
        System.out.println("Yes, the list is in descending order.");
    }
}

public static int getInt(Scanner sc) {
    if (sc.hasNextInt()) {
        return sc.nextInt();
    }
    sc.next();
    System.out.println("Type only numbers, please.");
    return getInt(sc);
}

If you want that the message prints after you stop typing numbers (by typing a value >= than 1000), the following could be a possible solution (equal values do not support descending order):

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Integer previousValue = getInt(sc);
    boolean descending = false;
    while(previousValue != null) {
        Integer currentValue = getInt(sc);
        if (currentValue != null) {
            if (previousValue > (previousValue = currentValue)) {
                if (!descending) { // if not yet true
                    descending = !descending; // set true
                }
            } else {
                descending = false; // to print: "No, ..."
                previousValue = null; // to break the loop
            }
        } else {
            previousValue = currentValue; // it's actually null
        }
    }
    System.out.println((descending)
        ? "Yes, the list is in descending order." 
        : "No, the list is not in descending order.");
}

public static Integer getInt(Scanner sc) {
    if (!sc.hasNextInt()) {
        sc.next();
        System.out.println("Type only numbers, please.");
        return getInt(sc);
    }
    int input = sc.nextInt();
    return (Math.abs(input) < 1000) ? input : null;
}

11 Comments

I am trying to compare x amount of numbers, the x value isnt given
says just a list of numbers
But do you mean it stops when next int is greater or equal to the previous one? Or maybe I don't understand the problem. I need more details.
@javahelp I'm sorry. You wrote 3 DIGIT numbers, and not 3 numbers ")
thanks for your time so given a list of three digit integrs, i need to make sure the list is in strictly descending order, if a number is not in descending order it should stop and print out no, the numbers are not in descending order and consecutive numbers that are equal are not in descneding order
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.