0

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 .

GhostCat
  • 137,827
  • 25
  • 176
  • 248
neetha
  • 31
  • 1
  • 5

6 Answers6

3
if(0<=h<=23 ||0<=m<=59||0<=ss<=59)

is not a valid syntax. You cannot chain <= operator.

You should compare one to one each operand and use && operator between the comparisons.

This

if(0<=h<=23)

should be :

if(0<=h && h<=23)

Anyway, it is not a straight and reliable way to do the task.
Use rather a DateTimeFormatter and instead int, use a String variable that will contain the input with the correct formatting :

StringBuilder dateInput = new StringBuilder();
System.out.println("Enter the number of test case");
int a = dateInput.append(s.next());

System.out.println("Enter the HH");
dateInput.append(s.next()).append(":");

System.out.println("Enter the MM");
dateInput.append(s.next()).append(":");

System.out.println("Enter the SS");
dateInput.append(s.next());

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

try{
   LocalTime time = LocalTime.parse(dateInput.toString(), formatter);
   System.out.println("Valid");
 }
 catch(DateTimeParseException e){
     System.out.println("Invalid");
 }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
2

As the other answers are pointing out clearly, you got your conditions wrong. It is simply wrong to go if (hour <= 0) to then print "valid".

But beyond those simple syntactic problems: you are re-inventing the wheel here. Unless this is some kind of exercise where you were asked to write such code yourself, you use the power of the Java date/time APIs:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String str = "13:42:10";
LocalTime time = LocalTime.parse(str, formatter);

The parse() call will throw an exception when the incoming string can't be parsed. See its javadoc. You also want to study the available patterns in detail.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

The error lies here, It has to be an and condition and not an or. Hour has to be below 24 and minutes have to be below 60 and seconds have to be below 60.

if(0<=h<=23 && 0<=m<=59 && 0<=ss<=59){
    System.out.println("Valid test case"+h+":"+m+":"+ss);
}
else
{
    System.out.println("invalid test case");
}
sameera sy
  • 1,708
  • 13
  • 19
0

You should just accept hh:mm:ss instead of putting it as 3 variables, and then validate it by trying to parse it to a Date.

Here is how:

System.out.println("Enter date as HH:MM:SS");
String a = s.nextString();
try { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    Date parsedDate = dateFormat.parse("12-12-2017 "+yourString);          
    // Parsing success, means a valid test case.
    System.out.println("Valid test case : "+a);
} catch(Exception e) {
    // Parsing failed, so the case is invalid!
    System.out.println("Invalid");
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Rudy
  • 7,008
  • 12
  • 50
  • 85
0

Use a SimpleDateFormat and set its lenient property to false. Then check whether the parse method returns null (this would indicate an invalid date). See API doc here: http://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#setLenient-boolean-

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Korgen
  • 5,191
  • 1
  • 29
  • 43
0

neetha.

Your code is incorrect. The validation must be like this:

if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && ss >= 0 && ss <= 59) {
    System.out.println("Valid test case" + h + ":" + m + ":" + ss);
}

Anyway, it would be better to use a class like LocalTime (if you use Java 8) in order to manage times:

LocalTime time = LocalTime.of(h, m, ss);

If the time is invalid, this code throws a java.time.DateTimeException.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
asmellado
  • 86
  • 1
  • 2