samedi 18 avril 2015

Adding user input to a map and incrementing integer key by 1

Fairly new to Java, and only just started doing maps, been trying for a few days with limited success to do the following, (comments in code) all suggestions welcome. Ideally, I would like to start from an unpopulated map and fill it purely with user input playerName and increment the integer value by one as each name is entered. Hope this follows all the guidelines!



/**
* Constructor for objects of class player Creates player
* objects and tests for unique player name
*/
public player() {
Map<String, Integer> playerMap = new HashMap<>();
System.out.println("Players in game: " + playerMap.isEmpty());
playerMap.put("Fred", 1);
playerMap.put("Bob", 2);
playerMap.put("Alice", 3);
playerMap.put("Charlotte", 4);
playerMap.put("Catherine", 5);
System.out.println("Players in game: " + playerMap.size());

/**
* user input via dialogue to get another playerName String
* playerName set to Catherine for testing
*/
String playerName = "Catherine";
boolean existing = playerMap.containsValue(playerName);
System.out.println(existing);
if (existing = true) {
int playerNumber = 5;
/**
* user input via dialogue to get another playerName I need
* to set playerNumber = highest existing key in map then
* increment by one then add values to map loop until all
* players have been entered, I am considering while
* (playerName != "last"); playerNumber set to 5 for
* testing
*/
playerNumber = playerNumber++;
playerMap.put(playerName, playerNumber);
}
}

Aucun commentaire:

Enregistrer un commentaire