Ben
Ben

Reputation: 21

User input never enters if statement

The point of the program is to have a user input an amount of integers endlessly (until they enter something other than an integer), and for each integer the user inputs, it should check if the integer is greater than or less than the value entered.

The problem: When the program runs, everything is fine until reaching

number = scanner.nextInt();

At this point, the user inputs their integer, but never makes it inside the following if statements. I would love a hint instead of an answer, but I'll take what I can get.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        do {
            System.out.println("Enter number: ");
            int number = 0;
            int minNumber = 0;
            int maxNumber = 0;
            boolean hasInt = scanner.hasNextInt();
            if (hasInt) {
                number = scanner.nextInt();
                if (maxNumber < number) {
                    maxNumber = number;
                }
                if (minNumber > number) {
                    minNumber = number;
                }
            } else {
                System.out.println("Your minimum number: " + number);
                System.out.println("Your maximum number: " + maxNumber);
                break;
            }
        } while (true);

        scanner.close();
    }
}

Upvotes: 0

Views: 401

Answers (2)

John Kugelman
John Kugelman

Reputation: 361625

It's not reaching the if statements, because if it did, the user input would update to the value entered I would think. It doesn't. It outputs the values initially declared.

You're not getting the right output and you have a hypothesis that the cause is the code not entering the if statements. Following the scientific method, the next step is to test your hypothesis.

If you put printouts inside the if statements you'll see that they are indeed running. That's not it. The mistake must be elsewhere. You should collect more evidence and develop a new hypothesis.

Hint: Try printing out the values of your variables at the beginning and end of each iteration. I've marked the places below. Are they what you expect them to be? You're going to see an anomaly that should point you in the right direction.

do {
    System.out.println("Enter number: ");
    int number = 0;
    int minNumber = 0;
    int maxNumber = 0;

    // Print number, minNumber, and maxNumber.

    boolean hasInt = scanner.hasNextInt();
    if (hasInt) {
        number = scanner.nextInt();
        if (maxNumber < number) {
            maxNumber = number;
        }
        if (minNumber > number) {
            minNumber = number;
        }
    } else {
        System.out.println("Your minimum number: " + number);
        System.out.println("Your maximum number: " + maxNumber);
        break;
    }

    // Print number, minNumber, and maxNumber.
} while (true);

Upvotes: 1

Hitesh A. Bosamiya
Hitesh A. Bosamiya

Reputation: 371

Your minNumber and maxNumber declarations should be out side of the loop. Also, you need to initialize the values as below to get correct min and max comparison with the entered values only:

int minNumber = Integer.MAX_VALUE;
int maxNumber = Integer.MIN_VALUE;

In print statement instead of minNumber you are printing number!

Upvotes: 4

Related Questions