Bug 608 - Input error for non integer value
Summary: Input error for non integer value
Status: RESOLVED FIXED
Alias: None
Product: UNO Game
Classification: Unclassified
Component: MessageDispalyer (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- normal
Assignee: zen268zzsyc
URL:
Depends on:
Blocks:
 
Reported: 2021-11-24 21:24 HKT by abbyshek2-c
Modified: 2021-11-24 21:54 HKT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description abbyshek2-c 2021-11-24 21:24:49 HKT
The code expect user to input card index for the card user want to play.

Currently, the code do not handle if player's input is not an integer.

It is needed to implement validation on user input.


-------------------------------------------------------
Exception in thread "main" java.lang.NumberFormatException: For input string: "@@"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:668)
        at com.cs3343.gp15.game.MessageDisplayer.getIndexInput(MessageDisplayer.java:109)
        at com.cs3343.gp15.game.GameManager.getInput(GameManager.java:125)
        at com.cs3343.gp15.game.GameManager.chooseCard(GameManager.java:113)
        at com.cs3343.gp15.game.GameManager.playerTurn(GameManager.java:79)
        at com.cs3343.gp15.game.GameManager.playerAction(GameManager.java:68)
        at com.cs3343.gp15.game.GameManager.gameProcess(GameManager.java:46)
        at com.cs3343.gp15.game.Main.main(Main.java:7)
Comment 1 abbyshek2-c 2021-11-24 21:54:13 HKT
Modified the function for getting input.

public int getIndexInput() {
	int input;
	try{
		input = Integer.parseInt(in.nextLine());
		return input;
	}catch(NumberFormatException e){
		System.out.println("Invalid input! Please enter the index of the card.");
		return getIndexInput();
	}
}

If receive invalid input, the function ask for next input and call itself again 

until user input the correct index.