In this assignment, we will be developing a set of methods to create "bulletproof " code for handling user input from a Scanner. This way, no matter what the user types, our code will not crash, nor throw an exception.
The following snippet of code is supposed to allow the user to input an integer and then will print the value to the console (naive implementation):
int number, min=1, max=10;
Scanner keyboard = new Scanner(System.{n);
System.out.print("Please enter a
System.out.printlln(”\nThe number you entered is: " + number);
keyboard.close();
First, create a new project for BulletproofValidation. Open the Main.java file and write the code above. Run this code in a main method, but enter text instead of an integer. The program should crash and tell you what type of exception was thrown by the nextlnt method. Wrap this code inside a try/catch block, where you catch the exception that is thrown. Add a loop, so the user must enter the number again if text is entered.
When complete, you should be able to generate the following transaction with the user
This assignment has been answered 4 times in private sessions.
© 2024 Codify Tutor. All rights reserved