5.14 Min and Max Challenge

Time for the next challenge: write a Java program that reads numbers from the console and, when the user enters an invalid value, exits the loop and prints the minimum and maximum number entered. For example, if the user types 5, 3 and 2, the minimum should be 2 and the maximum should be 5. Before each input, the program prompts "Enter a number". Use the Scanner class from the previous lessons.

The solution step by step

  • Create the Scanner and remember to call scanner.close() at the end.
  • Declare two variables: int min = Integer.MAX_VALUE; and int max = Integer.MIN_VALUE;. Starting from the extremes guarantees that the first valid input replaces them.
  • Use an infinite loop while (true) { ... }. To exit it as soon as the input is invalid, use a break.

Inside the loop, prompt the user, then check the type with boolean isAnInt = scanner.hasNextInt();. If isAnInt is false, we break out. Otherwise we read the value with int number = scanner.nextInt(); and update the bounds: if (number > max) max = number; and if (number < min) min = number;.

After the loop, we print the final minimum and maximum. Running the program and feeding it several numbers shows the values being updated as expected, until a letter or any non-integer breaks the loop and reveals the result. This pattern of "loop until invalid" combined with hasNextInt is a small but solid foundation for interactive Java console apps.

Summary

In this lesson, you learn to build a Min and Max Challenge program that reads multiple numbers from user input and calculates the minimum and maximum values entered. The program uses a Scanner class with an infinite while loop that continues until invalid input is detected. Input validation is performed using hasNextInt() to ensure only valid integers are accepted, and the min/max values are updated with each valid entry before displaying the results.

Key points

  • Use a Scanner class and infinite while loop (do-while) to read multiple numbers from users
  • Initialize min with Integer.MAX_VALUE and max with Integer.MIN_VALUE before the loop
  • Validate input using hasNextInt() method to detect invalid entries and exit the loop with break statement
  • Update max when a number is greater than the current max value
  • Update min when a number is less than the current min value
  • Display the final minimum and maximum values after the loop terminates

FAQ

How do you exit the infinite loop when invalid input is entered?

When hasNextInt() returns false (invalid input detected), use a break statement to exit the while loop immediately.

Why should min be initialized with Integer.MAX_VALUE and max with Integer.MIN_VALUE?

This ensures that any valid number entered will be smaller than the initial min value and larger than the initial max value, allowing the first entry to correctly set both variables.

What method is used to validate if the input is a valid integer?

The hasNextInt() method from the Scanner class checks if the next input is a valid integer before attempting to read it.