Bug 608

Summary: Input error for non integer value
Product: UNO Game Reporter: abbyshek2-c
Component: MessageDispalyerAssignee: zen268zzsyc
Status: RESOLVED FIXED    
Severity: normal    
Priority: ---    
Version: unspecified   
Hardware: PC   
OS: Windows   

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.