Reputation: 37
I haven't been programming for long and I have spent hours trying to get this method to do what I need so help would be very much appreciated at this point. I am creating a board game which prompts players for the number of players playing, after which they are prompted for their names in turn, and those new players will be created and their names set/stored. I also want to make sure that two players cannot have the same name.
Here is part of my player class:
public class Player {
private String name;
private int currentPosition;
private int distance;
private boolean quit;
public Player() {
}
public Player(String name, int currentPosition, int distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I have a setNumberOfPlayers method which is working fine and getting the correct number of players when getNumberOfPlayers() is called.
public class SetUpGame {
Scanner sc = new Scanner(System.in);
public void addPlayers() {
String playerName;
ArrayList<Player> players = new ArrayList<Player>();
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= getNumberOfPlayers(); loop++) {
System.out.println("Player " + loop + " please enter your name");
boolean valid = false;
do {
playerName = sc.nextLine();
Player player = new Player();
for (Player play : players) {
if (players.isEmpty() || !play.getName().equalsIgnoreCase(playerName)) {
player.setName(playerName);
players.add(player);
System.out.println("success, your name is: " +playerName);
valid = true;
} else {
System.out.println("please enter another name");
valid = false;
}
}
} while (valid == false);
}
sc.close();
}
When the add players method is called from main, it gives this output:
What are the names of the 2 players?
Player 1 please enter your name
...and the user can input their name but the scanner never closes or loops around to ask the next player their name. I have messed around with the code so much that I've confused myself at this point. Can anyone help with this, and also how to validate/check that the players have been created and their names set? Thank you
Upvotes: 2
Views: 1627
Reputation: 19565
First, list of players is defined as a local variable in the method addPlayers
, so when this method is done, all the players data are lost. So it is necessary to modify this method either to add the players to a field of SetupGame
class or to return the list of players to the calling method. The latter way is a cleaner functional way of populating the player list.
Next, in order to efficiently detect duplicate names, it is recommended to create and use Set<String> playerNames
.
And the last, if Scanner
is open on System.in
it must not be closed, because after that no user input can be entered.
That being said, the code of addPlayers
may look as follows:
public List<Player> addPlayers() {
List<Player> players = new ArrayList<>();
Set<String> playerNames = new HashSet<>();
Scanner sc = new Scanner(System.in);
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= this.numberOfPlayers; loop++) {
while (true) {
System.out.println("Player " + loop + " please enter your name");
String playerName = sc.nextLine();
if (playerNames.add(playerName)) {// Set.add returns true if name was added to it
Player p = new Player();
p.setName(playerName);
players.add(p);
break; // input new player
}
}
}
return players;
}
Upvotes: 1
Reputation: 73
You missed some getters and setters in your Player class.
Player.java
public class Player {
private String name;
private int currentPosition;
private double distance;
private boolean quit;
public Player() {
// no-args constructor
}
public Player(String name, int currentPosition, double distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public boolean isQuit() {
return quit;
}
public void setQuit(boolean quit) {
this.quit = quit;
}
}
You can implement the SetUpGame class as follows.
No need to create the do-while loop.
SetUpGame.java
import java.util.ArrayList;
import java.util.Scanner;
public class SetUpGame {
Scanner sc = new Scanner(System.in);
ArrayList<Player> players = new ArrayList<Player>();
int numberOfPlayers;
public int getNumberOfPlayers() {
return numberOfPlayers;
}
public void setNumberOfPlayers(int numberOfPlayers) {
this.numberOfPlayers = numberOfPlayers;
}
public void addPlayers() {
System.out.print("Enter number of players: ");
numberOfPlayers = sc.nextInt();
System.out.println("What are the names of the " + numberOfPlayers + " players?");
for(int loop = 1; loop <= numberOfPlayers; loop++) {
Player player = new Player();
System.out.print("Enter name for player " + loop + ": ");
player.setName(sc.next());
System.out.print("Enter current position for player " + loop + ": ");
player.setCurrentPosition(sc.nextInt());
System.out.print("Enter distance for player " + loop + ": ");
player.setDistance(sc.nextDouble());
System.out.print("Enter validity for player " + loop + ": ");
player.setQuit(sc.nextBoolean());
players.add(player);
}
sc.close();
}
public void displayAllPlayers() {
System.out.println("Displaying all players...");
for(Player p : players) {
System.out.printf("%s %d %f %b", p.getName(), p.getCurrentPosition(), p.getDistance(), p.isQuit());
}
}
}
Finally you can test your SetUpGame class.
TestSetUpGame.java
public class TestSetUpGame {
public static void main(String[] args) {
SetUpGame game = new SetUpGame();
game.addPlayers();
game.displayAllPlayers();
}
}
Upvotes: 0
Reputation: 514
Your inner for loop is iterating on players array, but it is empty.
Maybe try this instead:
do {
System.out.println("Please enter name:");
playerName = sc.nextLine();
if(playerName.length()!=0){
valid = true;
for(Player play : players)
if(play.getName().equalsIgnoreCase(playerName))
valid = false;
if(valid){
Player player = new Player();
player.setName(playerName);
players.add(player);
}
}
} while (!valid);
I think you might be having a hard time debugging because of how similar all your variable names are. ('players','player','play')
EDIT: updated to check if a duplicate
Upvotes: 1