Bug 771 - Application Crashes on Non-Numeric Input for User Choice
Summary: Application Crashes on Non-Numeric Input for User Choice
Status: RESOLVED FIXED
Alias: None
Product: Hospital System
Classification: Unclassified
Component: Login System (show other bugs)
Version: beta release
Hardware: PC Windows
: --- enhancement
Assignee: klhwong3-c
URL:
Depends on:
Blocks:
 
Reported: 2023-11-08 21:10 HKT by klhwong3-c
Modified: 2023-12-08 16:55 HKT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description klhwong3-c 2023-11-08 21:10:05 HKT
When the user is asked to input their choice to either log in or create a new account, entering a non-numeric value causes the application to crash with a NumberFormatException.
Comment 1 klhwong3-c 2023-11-08 21:11:17 HKT
Steps to Reproduce
1. Run the performLogin method in the application.
2. When presented with the options to "Login" or "Create new account", enter a non-numeric value (e.g., "a").

Expected Result
The application should handle non-numeric input gracefully, perhaps by displaying a message such as "Invalid input. Please enter a number." and then prompting the user to enter their choice again.

Actual Result
The application crashes with a NumberFormatException.

Suggested Fix
In the getUserChoice method, rather than directly parsing the input with Integer.parseInt(in.nextLine()), first check if the input is a number. If the input is not a number, return an error message and prompt for input again.

Screen dump:

--------------------------------------------------------
|   Welcome to the Hospital Management System (HMS)    |
|   Please select the following options:               |
|   1. Login                                           |
|   2. Create new account                              |
--------------------------------------------------------
a
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:668)
        at java.base/java.lang.Integer.parseInt(Integer.java:786)
        at com.cs3343.group1.hospitalSystem.RefactoredLogin.getUserChoice(RefactoredLogin.java:46)
        at com.cs3343.group1.hospitalSystem.RefactoredLogin.performLogin(RefactoredLogin.java:25)
        at com.cs3343.group1.hospitalSystem.RefactoredLogin.login(RefactoredLogin.java:10)
        at com.cs3343.group1.hospitalSystem.hospital.HospitalSystem.start(HospitalSystem.java:103)
        at com.cs3343.group1.hospitalSystem.Main.main(Main.java:11)