I am currently working as a software tester. I have a scenario to test a time format.
I want to enter a valid time format using java program. I tired but couldn't get a good answer.
I want to enter the time(hh:mm:ss) in correct format. It accepts the time as specified by the format HH:MM:SS. time should be separated by colons and excpet colons no other characters like /,- should be print invalid . And hour range from 0 to 23, minute range from 0 to 59 and second should be range from 0 to 59.
And only numbers are acceted, if I enter character it will print invalid time format.
And these 2 values 20:62:00 and 20:80:00, also invalid because minute should range from 0 to 59.
This is I tried :
import java.util.Scanner;
public class AlarmTime {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of test case");
int a = s.nextInt();
System.out.println("Enter the HH");
int h = s.nextInt();
System.out.println("Enter the MM");
int m = s.nextInt();
System.out.println("Enter the SS");
int ss = s.nextInt();
System.out.println("Time is" + h + ":" + m + ":" + ss);
if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && ss >= 0 && ss <= 59) {
System.out.println("Valid test case" + h + ":" + m + ":" + ss);
} else {
System.out.println("invalid test case");
}
}
}
Output:
Enter the number of test case
4
Enter the HH
22
Enter the MM
33
Enter the SS
44
Time is22:33:44
Valid test case22:33:44
Process finished with exit code 0
Here I wrote the test cases as 4 and then again how to write the next test case 2. and also I need to write a test case for invalid input like enter characters .