1

I have an array of Player objects. The players have names and when i add a player, i want to check if the playername already exists. Following code never throws the exception, it just adds duplicate players.

public void addPlayer(String name, boolean gender, int index) throws RuntimeException {
 List<String> names = new ArrayList<>();
 if (names.contains(name))
 throw new DuplicatePlayerException();
 else {
 players[index] = new Player(name, gender);
 names.add(name);
 }
}
Amadeu Antunes
7261 gold badge8 silver badges28 bronze badges
asked Mar 5, 2019 at 9:38
4
  • What is players[index]? Commented Mar 5, 2019 at 9:41
  • issue in this line List<String> names = new ArrayList<>(); every time you create new ArrayList. Commented Mar 5, 2019 at 9:41
  • Because every time you create new List<String> names which is empty. It should be a class-level-variable Commented Mar 5, 2019 at 9:41
  • Using a boolean for gender is somehow from the last century... names cannot contain the given name in the method because it will always be empty before your if statement that checks if the name is contained. Make it a class attribute... Commented Mar 5, 2019 at 9:41

3 Answers 3

9
public void addPlayer(String name, boolean gender, int index) throws RuntimeException {
 List<String> names = new ArrayList<>(); // you create a new instance of the list each time you call it, so it'll always be empty
 if (names.contains(name)) // your empty list does not contain anything
 throw new DuplicatePlayerException();
 else {
 players[index] = new Player(name, gender);
 names.add(name);
 }
}

You'll need to change your method, to work with an instance level list:

private List<String> names = new ArrayList<>();
public void addPlayer(String name, boolean gender, int index) throws RuntimeException {
 if (names.contains(name))
 throw new DuplicatePlayerException();
 else {
 players[index] = new Player(name, gender);
 names.add(name);
 }
}

so the contents of names won't be erased each time you call your method.

answered Mar 5, 2019 at 9:42
1

You could consider Set<Player> instead of array. Set by definition can not contain the duplicates. Assuming Player has implemented equals/hashcode your code might look like:

Set<Player> players = new HashSet<>();
public void addPlayer(Player player) throws RuntimeException {
 if (!players.add(player)) {
 throw new DuplicatePlayerException();
 }
}

Set::add returns true if the set did not already contain the element

answered Mar 5, 2019 at 9:47
3
  • even though this is indeed a good remark, if he had used a Set instead of a List, he still would have had the same problem Commented Mar 5, 2019 at 9:53
  • @Stultuske I suggest to use Set instead of array. There is no List in my answer Commented Mar 5, 2019 at 10:43
  • I was referring to the code of the OP, where he used a list. In your answer, you fixed his error, yet you don't speak of that, you only explain the benefit of using a Set (after fixing his error) Commented Mar 5, 2019 at 10:47
0
String Names = "John";
 if (Names.toLowerCase().contains("john")){
 System.out.println("yes");
 }

You can also use .toLowerCase().contains() to include case sensitive inputs.

answered Mar 5, 2019 at 9:53
1
  • This would make a good comment, but it is not related to the actual problem the OP encountered. Commented Mar 5, 2019 at 9:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.