In some programs, user input could be more complex than a number, boolean or string. This makes it easier for users to enter commands without answering multiple prompts.
Examples:
Let's take the first example as a starting point: When a user is asked for exactly two integers, separated by white space, your program could throw an exception if they don't enter valid input. For example consider the following invalid responses:
In order to handle invalid input gracefully, you will need to make sure the input is valid before trying to parse it. One way to do this for the above example is to do the following:
In order to return the input, you will need a class that stores all the data. In this case, the input is two integers, so we need a class that stores two int values.
Make a Move class (named appropriately, for example P1_Wang_Michael_Move.java) with:
Make a driver class (named appropriately: PX_LastName_FirstName_MoveDriver.java) that tests your Move class. The driver should have a main method that creates a Scanner for reading System.in and then keeps doing the following:
Enter two integers: 3 5
You chose (3, 5)
Type q to quit or anything else to continue: sdfg
Enter two integers: 3
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 5 6 7
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 23 456
You chose (23, 456)
Type q to quit or anything else to continue:
Enter two integers: two five
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 23f 1
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 23 4 more
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers:
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: one
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 12
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers:
Invalid format. Please enter exactly two integers separated by a space.
Enter two integers: 123 87645
You chose (123, 87645)
Type q to quit or anything else to continue: q
Test complete.
All files must incude your name and period in the format
PX_LastName_FirstName_Title
You must Sign In to submit to this assignment
Last modified: December 12, 2022
Back to Lab 10.5 Romannumerals