As everyone said, regex
is the best option. I will provide you with a simple method. I will use BufferedReader as an example here. Feel free to use any input stream
.
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
For n inputs simply accept n and start a loop.
int n = Integer.parseInt(bufferedReader.readLine().trim());
for (int i = 0;i < n;i++)
Each time you will accept each line of input consisting both date of arrival and departure as a single expression and split it using regex into 6 parts using delimiters
:
and whitespace
.
Eg: 12:00 AM 11:42 PM will be split into and array of Strings consisting of elements [12, 00, AM, 11, 42, PM].
String[] in = bufferedReader.readLine().split("[: ]");
//Array L to store HH MM XX of date of arrival at respective index
String[] L = new String[3];
L[0] = in[0];
L[1] = in[1];
L[2] = in[2];
//Array R to store HH MM XX of date of departure at respective index
String[] R = new String[3];
R[0] = in[3];
R[1] = in[4];
R[2] = in[5];
This approach will help you if want to perform any further calculations. You can also cast them to Integer
using Integer wrapper class
which will ease you in any type of arithmetic or comparative calculation. It is simple and straightforward, though a bit long.
I hope I have helped you.