javahelp
javahelp

Reputation: 37

having runtime error with java program

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() ;
        }
    }

}

Upvotes: 1

Views: 154

Answers (2)

LowLevel
LowLevel

Reputation: 1095

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;
}

Upvotes: 0

Josh Wilson
Josh Wilson

Reputation: 3745

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.

Upvotes: 1

Related Questions