Class IOController

java.lang.Object
utils.IOController

public class IOController extends Object
Utility class providing static methods for handling console input operations. Encapsulates reading integers, lines of text, passwords (securely if possible), and dates from standard input (System.in). Includes basic validation and re-prompting mechanisms for invalid input.
  • Constructor Details

    • IOController

      public IOController()
  • Method Details

    • nextInt

      public static int nextInt()
      Reads the next integer value from the console input. If the user enters non-integer input, it catches the exception, prints an error message prompting for a valid integer, consumes the invalid input line, and recursively calls itself until a valid integer is entered. Also consumes the trailing newline character after successfully reading an integer.
      Returns:
      The valid integer entered by the user.
    • nextLine

      public static String nextLine()
      Reads the next complete line of text (up to the newline character) from the console input.
      Returns:
      The line of text entered by the user.
    • readPassword

      public static String readPassword()
      Reads a password securely from the console, if available. Attempts to use System.console() to read the password without echoing characters. If the console is not available (e.g., running in some IDEs), it falls back to reading a normal line of text using nextLine().
      Returns:
      The password entered by the user as a String. Be aware that the fallback method is not secure.
    • nextDate

      public static LocalDate nextDate()
      Prompts the user to enter a date (day, month, year separately as integers) and constructs a LocalDate object. If the entered combination results in an invalid date (e.g., Feb 30th), it catches the DateTimeException, prints an error message, and recursively calls itself until a valid date is entered.
      Returns:
      The valid LocalDate entered by the user.