Java Bootcamp – Workbooks and Challenges
)}
Exception Handling

Part 2

From the Java Bootcamp Resources, launch Part 2.

2.png

Task 0

The loadMovies method is already implemented.

public static void loadMovies(String fileName) throws FileNotFoundException { // reads input from file... }

If you want to visualize its runtime, feel free to watch the video solution.

Task 1

This method should return true if the choice is less than 0 or greater than 9.

public static boolean incorrectChoice(int choice) { // TODO }

Task 2

This method should return true if the rating is less than 0 or greater than 10.

public static boolean incorrectRating(double rating) { // TODO }

Task 3

The method promptForChoice will return the integer they choose.

public static int promptForChoice(Scanner scanner) { while (true) { System.out.print("\nPlease choose an integer between 0 - 9: "); int choice = scanner.nextInt(); return choice; <-------------- } }

Scenario One: continue the while loop if the user doesn't enter an integer.

public static int promptForChoice(Scanner scanner) { while (true) { System.out.print("\nPlease choose an integer between 0 - 9: "); // 1. Anticipate the user not entering an integer. <-------------- int choice = scanner.nextInt(); return choice; } }

Scenario Two: continue the while loop if the user enters an incorrect choice.

public static int promptForChoice(Scanner scanner) { while (true) { System.out.print("\nPlease choose an integer between 0 - 9: "); // 1. Anticipate the user not entering an integer. int choice = scanner.nextInt(); // 2. Anticipate the choice being incorrect. <-------------- return choice; } }

Task 4

The method promptForRating will return the rating they choose.

public static double promptForRating(Scanner scanner, String name) { while (true) { System.out.print("\nSet a new rating for " + name + ": "); double rating = scanner.nextDouble(); return rating; <--------- } }

Scenario One: continue the while loop if the user doesn't enter a double.

public static double promptForRating(Scanner scanner, String name) { while (true) { System.out.print("\nSet a new rating for " + name + ": "); // 1. Anticipate the user not entering a double. <------------- double rating = scanner.nextDouble(); return rating; } }

Scenario Two: continue the while loop if the user enters an incorrect rating.

public static double promptForRating(Scanner scanner, String name) { while (true) { System.out.print("\nSet a new rating for " + name + ": "); // 1. Anticipate the user not entering a double. double rating = scanner.nextDouble(); // 2. Anticipate the rating being incorrect. <---------- return rating; } }

Associated Course: The Complete Java Development Bootcamp
Related Course: The Complete Spring Boot Development Bootcamp – Become a Java Web Developer

Feedback Summary
4.7
43 students
5

91%
4

2%
3

0%
2

0%
1

7%
Written Reviews
There are no written reviews yet.