If the conversion failed, the code outputs and error message and requests the user to re-enter a proper input.
First, we set up a method to test whether or not the accepted input can be converted to a specific type: The loop is called with a string parameter that decides which message the user will see.
Input validation is performed to ensure only properly formed data is entering the workflow in an information system, preventing malformed data from persisting in the database and triggering malfunction of various downstream components.
From a broader view, you’ll write no less than 1700 lines of code just for your validations and all that is repetitive.
The second solution that I came up with was to find out a way to make the fields required by some means, and check at runtime if the required fields have been initialised or not.
The class methods accept user input as string, attempt to convert that input to the expected type.
If the conversion was successful, the method returns the result in the correct type.