zachtaylor/JPokemon — Issue #2: Tools.askForInput Usage
Author: shortstuffsushi
Created: 2012-02-24 04:35:00+00:00
State: open

Hi Zach,

I was just browsing through the code in the Tools.java, and noticed you're using the expected string up to index 6 for both this method below, and select move (as a cancel option). I don't know if you had the same intent in this method (to cancel) or if you just missed that, but index 6 would be out of bounds.

Also, rather than using the 'out of bounds' index to cancel, you could just modify the "ask for input" to (optionally) accept null, indicating they wish to cancel.

/**
* Return position of selected party member. Only show buttons for members
* that are != null.
*
* @param userparty The party to survey
* @return Position of Pokemon in party (0-5). -1 if cancel.
*/
public static int selectFromParty(String message, Party userparty) {
StringBuilder list = new StringBuilder("Party List:\n");
String expectedOption = "0|1|2|3|4|5|6";
int partyCount = 0;

// We have to build the message to vary depending on
// how many moves the active Pokemon has
for (int i = 0; i < userparty.size(); ++i) {
if (userparty.pkmn[i] != null) {
partyCount++;
list.append(i + ": " + userparty.pkmn[i].toString() + "\n");
}
}

return Integer.parseInt(Tools.askForInput(message, list.toString(),
// From 0 to 2 x The number of moves, - 1 to drop last |
expectedOption.substring(0, partyCount * 2 - 1)));
}