Class IOController
java.lang.Object
utils.IOController
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDatenextDate()Prompts the user to enter a date (day, month, year separately as integers) and constructs aLocalDateobject.static intnextInt()Reads the next integer value from the console input.static StringnextLine()Reads the next complete line of text (up to the newline character) from the console input.static StringReads a password securely from the console, if available.
-
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
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
Reads a password securely from the console, if available. Attempts to useSystem.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 usingnextLine().- Returns:
- The password entered by the user as a String. Be aware that the fallback method is not secure.
-
nextDate
Prompts the user to enter a date (day, month, year separately as integers) and constructs aLocalDateobject. If the entered combination results in an invalid date (e.g., Feb 30th), it catches theDateTimeException, prints an error message, and recursively calls itself until a valid date is entered.- Returns:
- The valid
LocalDateentered by the user.
-