Skip to content

Exception: Data type mismatch thrown in User Input Validation #1

@sdley

Description

@sdley

In the current implementation, user input is read using Scanner.nextInt() for age and Scanner.nextLine() for name fields in console applications. If the user provides an invalid data type (e.g., entering 'hello' instead of a number for age), the application throws a java.util.InputMismatchException and terminates unexpectedly. This behavior reduces the robustness and user-friendliness of the application. The issue is present in the use cases for age input (expects integer) and name input (expects non-empty string).

Steps to Reproduce

  1. Run the application.
  2. When prompted with Enter your age (must be a positive number):, enter a string (e.g., hello) instead of an integer.
  3. Observe the InputMismatchException and stack trace in the console.

Expected Behavior

The program should validate the input and prompt the user to try again with the correct data type, displaying a clear error message such as "Invalid input. Please enter a valid number for age." The application should not crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions