Hope you're doing well. So 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);
}
Looking for guidance, Thank you in advance.
So 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.
CodePudding user response:
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.
CodePudding user response:
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");
}
}
}