I am writing a simple Pokemon-like program but I am encountering an issue when the user specifies how many Pokemon they want on their team. A user's team has a Map of The Pokemon's name and the Pokemon object itself.
When the game begins the user specifies how many Pokemon they want on their team. There is a pre-made array of 6 Pokemon. The default Pokemon constructor assigns the name to a random double between 2e14 and -2e14. Then, a for loop adds the specified number of Pokemon objects to the user's team and asks for the stats for each one. The loop asking for the stats then is supposed to, for each entry, remove it and put back in an entry with the key corresponding to the inputted pokemon name.
I currently have the following issues: - ConcurrentModificationException
EDIT: Solved. I instead took the stats using the objects in the pre-made possible pokemons array, then once they had stats, added them to the team so I didn't have to change the Key. HERE IS THE OFFENDING CODE:
// for every Pokemon on the user's team, get the stats for them.
// Logic:
/* The for loop goes through the Map. For each entry, it saves the name of the pokemon
* and the pokemon itself, because if you don't, the key is just a random double, so
* the user can't call its name. This goes through and removes those entries,
* then re - inserts them back into the team, but this time the key corresponds to its name */
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
// if the pokemon hasn't already gone through this procedure,
if(!entry.getValue().hasBeenStats) {
entry.getValue().getStats(input);
Pokemon pok = entry.getValue();
userTeam.team.remove(entry.getKey());
userTeam.addPokemon(pok);
}
}
Here are some additional code snippets if you want:
public class PokemonGame {
int userTeamSize;
PokemonTeam userTeam = new PokemonTeam();
// potential pokemon
Pokemon pok1 = new Pokemon();
Pokemon pok2 = new Pokemon();
Pokemon pok3 = new Pokemon();
Pokemon pok4 = new Pokemon();
Pokemon pok5 = new Pokemon();
Pokemon pok6 = new Pokemon();
// 0 1 2 3 4 5
Pokemon[] potentialUserPokemons = {pok1, pok2, pok3, pok4, pok5, pok6};
Here are the methods that are supposed to receive a number that will be the teamSize then add that number of Pokemon's from the array to the user's team:
private void getUserSettings(Scanner input, PokemonTeam team) {
System.out.println("How many Pokemon do you want on your team?");
while(true) {
try {
int tempInt = Integer.parseInt(input.next());
if((tempInt > 6) || (tempInt < 1) ) {
System.out.println("Error: Enter valid team length.");
continue;
} else {
userTeamSize = tempInt;
}
break;
} catch(NumberFormatException e) {
System.out.println("Error: Try again");
continue;
}
}
input.nextLine();
}
private void setUpUserTeam(Scanner input) {
/* adds every Pokemon for the specified length into the users team, from the
* potential team array*/
for(int num = 0; num < userTeamSize; num++) {
userTeam.addPokemon(potentialUserPokemons[num]);
}
// for every Pokemon on the user's team, get the stats for them.
// Logic:
/* The for loop goes through the Map. For each entry, it saves the name of the pokemon
* and the pokemon itself, because if you don't, the key is just a random double, so
* the user can't call its name. This goes through and removes those entries,
* then re - inserts them back into the team, but this time the key corresponds to its name */
// this part is not working, it only run once
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
// if the pokemon hasn't already gone through this procedure,
entry.getValue().getStats(input);
// save it's name --> key, and object --> pokemon for the value
String pokName = entry.getValue().getName();
Pokemon pok = entry.getValue();
userTeam.team.put(pokName, pok );
userTeam.team.remove(entry.getKey());
}
System.out.println("Pick a Pokemon to start with: ");
String pickedPokemon = input.nextLine();
// goes through the user's team, finds the Pokemon they specified, and sets it as the current pokemon
outerloop:
while (true) {
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
if(entry.getKey().equals(pickedPokemon)) {
userTeam.setCurrentPokemon(entry.getValue());
break outerloop;
}
}
System.out.println("Error: Pokemon not found. Try again.");
}
}
And in PokemonTeam, there is a Map and a method that adds Pokemon to it:
Map<String, Pokemon> team = new HashMap<String, Pokemon>();
public void addPokemon(Pokemon pokemon) {
team.put(pokemon.getName(), pokemon);
/*teamSize is a different variable in PokemonTeam and once the
* Pokemons are added to the Map, will be the same as userTeamSize
* in class PokemonGame*/
teamSize = team.size();
}
Here is getStats() from Pokemon Class:
public void getStats(Scanner theInput) {
System.out.println("Please enter the stats of your pokemon: ");
System.out.println("Name: ");
// set the pokemon's name as what they enter
this.setName(theInput.nextLine());
// error handling
System.out.println("Level: ");
while(true) {
// if there is a wrong type entered it will repeat until correct
try {
this.setLevel(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
System.out.println("Attack: ");
while(true) {
try {
this.setAttack(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
System.out.println("Defense: ");
while(true) {
try {
this.setDefense(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
System.out.println("Base: ");
while(true) {
try {
this.setBase(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
System.out.println("STAB: ");
while(true) {
try {
this.setSTAB(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
System.out.println("HP: ");
while(true) {
try {
int userHP = Integer.parseInt(theInput.next());
this.setMaxHP(userHP);
this.setCurrentHP(userHP);
this.setDamageAuto();
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
// gets the names of the moves and adds them to the map of moves and move infos
theInput.nextLine();
System.out.println("Name your Pokemon's 4 moves: ");
String moveNameOne = theInput.nextLine();
moves.put(moveNameOne, generateMoveInfo(moveNameOne));
String moveNameTwo = theInput.nextLine();
moves.put(moveNameTwo, generateMoveInfo(moveNameTwo));
String moveNameThree = theInput.nextLine();
moves.put(moveNameThree, generateMoveInfo(moveNameThree));
String moveNameFour = theInput.nextLine();
moves.put(moveNameFour, generateMoveInfo(moveNameFour));
hasBeenStats = true;
}