Bug 771

Summary: Application Crashes on Non-Numeric Input for User Choice
Product: Hospital System Reporter: klhwong3-c
Component: Login SystemAssignee: klhwong3-c
Status: RESOLVED FIXED    
Severity: enhancement CC: hongjlee2-c
Priority: ---    
Version: beta release   
Hardware: PC   
OS: Windows   

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)