-1

I just get the time in HH:MM format and check if it is greater than 9:30 then count c is increased as 1.I just did for a single user input time.But i need to get multiple times from user and compare.If it is greater than 9:30 then increment the count values.First get n value and then get n no of time from user.How can i change my code to get the n no of time and compare that?

Scanner input = new Scanner(System.in);
 String time = input.nextLine();
 System.out.println();
 int c=0;
String time2 = "9:30";
 DateFormat sdf = new SimpleDateFormat("hh:mm");
 Date d1 = sdf.parse(time);
 Date d2 = sdf.parse(time2);
 if(d1.after(d2))
 {
     c++;
}
System.out.println(c);
Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
Harini
  • 195
  • 2
  • 8
  • 2
    Loops are your friend – Benjamin Urquhart Jun 13 '19 at 16:20
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead just use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 13 '19 at 16:41
  • Would 12:15 count as after 9:30? I entered `12:15`, and your snippet printed `0`. It’s not that much of your fault, it’s `SimpleDateFormat` misbehaving. – Ole V.V. Jun 13 '19 at 18:30

2 Answers2

1

Use for loop to iterate over the list of time. Also, you do not need n value, you can directly get it with list.size()

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

1

This should do it. It is a basic implementation, you can optimize it the way you like.

EDIT (with explanation comments):

Scanner sc = new Scanner(System.in);

// accept user input for N
System.out.println("Enter N");
int n = sc.nextInt();

String time;
int c = 0;

// store the DateFormat to compare the user inputs with
String time2 = "9:30";
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date d2 = null;
try {
    d2 = sdf.parse(time2);
} catch (ParseException e) {
    e.printStackTrace();
}

// iterate for N times, asking for a user input N times.
for (int i = 0; i < n; i++) {
    // get user's input to parse and compare
    System.out.println("Enter Time");
    time = sc.next();
    Date d1 = null;
    try {
        d1 = sdf.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (d1.after(d2))  {
        c++;
    }
}
System.out.println(c);

I have not changed much of your code, just added a loop and did the same thing for N times. To quote from the comments above, "loops are your friend".

Hope this helps. Good luck. Comment if you have any further questions.

Harshal Parekh
  • 4,830
  • 4
  • 13
  • 34
  • It works.....But why did you used try catch?Could you please explain it? – Harini Jun 13 '19 at 16:35
  • Try catch is used to adopt defensive programming. What if the string entered by the user is not parse-able in the DateFormat? Also, without try-catch it will be a compile time error. – Harshal Parekh Jun 13 '19 at 16:37
  • if we use sc.nextLine() it shows exception.Why can't we use that? – Harini Jun 13 '19 at 17:03
  • https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class this will help. – Harshal Parekh Jun 13 '19 at 17:07