I am trying to get a string input from user, in which the input format is "HH:MM am/pm", for example "9:10 am"
The input in which I will later use it for displaying the hours, minutes and the am or pm (in my Time class).
Eventually I will implement a try...except..
for checking the input (for the hours/mins, it must be numeric, between the values of 0-12, and only am/pm can be used).
Currently my code works only if user input in HH:MM am/pm
(not the space in between) and fails if the input, eg. 9:10pm
due to the lack of whitespace.
My question - Can I enforce the input, such that the user has to follow a certain convention? In this case, there must be a space between the time and the am/pm?
In my client class, the code is as follows:
class Client
{
public static void main(String[] args)
{
// I have removed the Scanner input method and use hardcoded values for now
String input = "9:10 pm";
String[] inputSplit = input.split(" ");
System.out.printf("\ntime:\t%s\n", inputSplit[0]);
System.out.printf("am/pm:\t%s\n", inputSplit[1]);
String[] time = inputSplit[0].split (":");
int hour = Integer.parseInt (time[0].trim() );
int min = Integer.parseInt (time[1].trim() );
System.out.printf("hr:\t\t%d\n", hour);
System.out.printf("mins:\t%d\n", min);
}
}