0

Recently, I was given a problem statement to basically enter movie title, release date and their rating based on IMDB and then sort any of the two properties, which was done easily. Now, while entering the date, I was getting a parse error, so I used try and catch block. Now the problem arising here is when let's say I give a wrong value, it says you've entered the wrong date format, re-enter the details. It's not taking the re-entered value, but instead null. Here is the code.

boolean movieQuestion;
while(true){
    System.out.println("Do you want to enter movie rating yes or no? ");
    String inputString = scanner.next();
    movieQuestion =inputString.equalsIgnoreCase("Yes");
    if(!movieQuestion){
        System.out.println("Come again Later!");
        break;
    }

    System.out.println("Enter the name of the movie: ");
    String movieName = scanner.next();
    System.out.println("Enter the movie rating(out of 10):");
    double rating = scanner.nextDouble();

    Date movieDate = null;
    try {
        System.out.println("Enter the release date(dd-mm-yyyy)");
        String inputDate = scanner.next();
        movieDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputDate);
    } catch (ParseException e){
        System.out.println("You've entered the wrong date format");
        System.out.println("Enter the release date(dd-mm-yyyy)");
        String inputDate = scanner.next();
    }

            Movie movie = new Movie(movieName,movieDate,rating);
    mainMovieSet.add(movie);

}

This is what I tried to avoid breaking of code with an exception.

Date movieDate = null;
try {
    System.out.println("Enter the release date(dd-mm-yyyy)");
    String inputDate = scanner.next();
    movieDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputDate);
} catch (ParseException e){
    System.out.println("You've entered the wrong date format");
    System.out.println("Enter the release date(dd-mm-yyyy)");
    String inputDate = scanner.next();
}

Every time I re-enter the value if a wrong value is given, it just displays a null after the correct value displayed.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • See [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) Specifically, when you use scanner.next...(); you only get the next element and don't consume the entire line, so the very next call to scanner.next...(); will grab the next element on the previous line which will typically be blank or a newline character, and therefore null. Always use `scanner.nextline();` where possible, or consume the rest of the line after any "next" calls other than nextLine. – sorifiend Mar 22 '22 at 03:08
  • In catch block you are not assigning movieDate as its declare null intialy its taking the same add this in catch block also String inputDate = scanner.next(); movieDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputDate); – bhaskarkh Mar 22 '22 at 04:12

2 Answers2

0

I've found the answer for this problem.

Date movieDate = null;
while( movieDate == null ) {
    try {
        System.out.println("Enter the release date(dd-mm-yyyy)");
        String inputDate = scanner.next();
        movieDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputDate);
    } catch (ParseException e){
        System.out.println("You've entered the wrong date format");
    }
}

The previous code the error was @ catch I was using the "inputDate" as a Local variable.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Hi @Mritunjay.Upadhyay the problem is that you use the movieDate that is null in the exception case, rather than skipping creating the movie with the null date. What you did with the while is one way of fixing the problem. – Mr R Mar 22 '22 at 09:14
  • Hi Mr R, Thank you for your advice. I'm a fresher in programming. I did not think it like the way you suggested. Could you show how it's done? Would really appreciate it. – Mritunjay Upadhyay Mar 22 '22 at 10:13
0

The core problem you encountered was one of scope. In the catch-block you prompt and accept a new response but you capture it in a variable defined within the catch block. When the block ends, the variable is no longer in scope and the value is lost. You never parse a Date from the String so the Date variable is still null.

Consider changing the logic to redirect back to the input step if you get an exception. The following example places all of this in its own method to isolate the unit of work. Within the method, use a loop to allow us to repeatedly prompt the user for the date input. We place all of the core logic in the try-block. The catch-block is only responsible for informing the user it was a bad parse.

private Date getReleaseDate() {
  while (true) {
    try {
      System.out.println("Enter the release date (dd-mm-yyyy):");
      String inputDate = scanner.next();
      return new SimpleDateFormat("dd-MM-yyyy").parse(inputDate);

    } catch (ParseException e) {
      System.out.println("You've entered the wrong date format, please try again");
    }
  }
}
vsfDawg
  • 1,425
  • 1
  • 9
  • 12