Hi Guys new to java and new to this forum.
This place is great for tips and help with Java.
I am currently stuck on a question I have.
The public instance method getNumOfMoves() takes no arguments and returns an integer between 1 and 3 (inclusive).
getNumOfMoves() makes use of two helper methods, promptForNumOfMoves(), which prompts the user for a number between 1 and 3 and attempts to parse the response as an integer, and isValidNumOfMoves(), which checks that the number the user has input is in the correct range 1 to 3 (inclusive). If the input is out of range, the user is prompted again, and the method continues to loop until a number in the required range is entered.
But what if the user makes a mistake and enters something that cannot be parsed as an integer at all? In this case, the parseInt() method will throw an exception and the program will simply stop executing.
To handle this case more gracefully, the promptForNumOfMoves() method can be modified to make use of a try-catch block. If an exception is thrown, the catch statement can assign 0 to the number of moves, and since this is outside the range 1 to 3 the user will be prompted to try again. (Of course any integer outside the required range could be used instead of 0 but it seems a natural choice.)
Modify the promptForNumOfMoves() method so that it handles the possible exception in the way described above.
This is what I have
*/
public int promptForNumOfMoves()
{
int moves;
moves = Integer.parseInt(OUDialog.request
("Please enter the number of dance moves to be performed - between 1 and 3 (inclusive)"));
return moves;
}
Any tips or help with this would be great
Thanks
This place is great for tips and help with Java.
I am currently stuck on a question I have.
The public instance method getNumOfMoves() takes no arguments and returns an integer between 1 and 3 (inclusive).
getNumOfMoves() makes use of two helper methods, promptForNumOfMoves(), which prompts the user for a number between 1 and 3 and attempts to parse the response as an integer, and isValidNumOfMoves(), which checks that the number the user has input is in the correct range 1 to 3 (inclusive). If the input is out of range, the user is prompted again, and the method continues to loop until a number in the required range is entered.
But what if the user makes a mistake and enters something that cannot be parsed as an integer at all? In this case, the parseInt() method will throw an exception and the program will simply stop executing.
To handle this case more gracefully, the promptForNumOfMoves() method can be modified to make use of a try-catch block. If an exception is thrown, the catch statement can assign 0 to the number of moves, and since this is outside the range 1 to 3 the user will be prompted to try again. (Of course any integer outside the required range could be used instead of 0 but it seems a natural choice.)
Modify the promptForNumOfMoves() method so that it handles the possible exception in the way described above.
This is what I have
*/
public int promptForNumOfMoves()
{
int moves;
moves = Integer.parseInt(OUDialog.request
("Please enter the number of dance moves to be performed - between 1 and 3 (inclusive)"));
return moves;
}
Any tips or help with this would be great
Thanks
Comments
Post a Comment