3

I am a java beginner and I am designing a Nim game for many players to join. I've done some research but I don't know if my implementation is correct. The aim is to check the duplicate object in an array. I've already checked some articles, and I'll reference them in the last part of this article.

For the NimPlayer class. I've created some things.

  1. I've defined the NimPlayer object type.
  2. Using the type, I can initialize the player in the limited space.
  3. I initialize an array for saving player's data by following the steps here: Storing object into an array - Java

    public class NimPlayer {
    String userName;
    String familyName;
    String givenName;
    NimPlayer [] playerList = new NimPlayer[10]; //set an array here
    int id;
    
    //define NimPlayer data type
    public NimPlayer(String userName,String surName, String givenName) {
        this.userName = userName;
        this.familyName = surName;
        this.givenName = givenName;
    }
    //create new data using NimPlayer data type
    public void createPlayer(String userName, String familyName, String givenName) {   
        playerList[id++] = new NimPlayer(userName, familyName, givenName);
    }
    

In the main method, I have created some features for players to use:

  1. addplayer - let the user can add players in the game to compete.
  2. To add the player, the Syntax like this:

    $addplayer userName,familyName,givenName
    
  3. to validate the input, I split the input and store them in the new object.

    public static String[] splitName(String inputName) {
    String [] splittedLine = inputName.split(",");
    String userName = splittedLine[0].trim();
    String familyName = splittedLine[1].trim();
    String givenName = splittedLine[2].trim();
    String [] name = new String[3];
    name[0] = userName;
    name[1] = familyName;
    name[2] = givenName;
    return name;
    }
    
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    //create new object to save data
    NimPlayer playerData = new NimPlayer(null, null, null);
    
    System.out.print('$');
    String commandInput = in.next();
    
    while (true) {
    
        if (commandInput.equals("addplayer")) {
            String inputName = in.nextLine();
            String[] name = splitName(inputName);
            String userName = name[0];
            String familyName = name [1];
            String givenName = name[2];
    
            playerData.createPlayer(userName, familyName, givenName);
    
            for (int i = 0; i < playerData.playerList.length; i++) {
                NimPlayer player = playerData.playerList[i];
                System.out.println(player.getUserName()); }
    }
    

    So far, I have two questions here.

  4. Every time I enter a set of data, it seems my "playerData" provokes the NullPointerException when looping through the object, but since my name input is multiple, I have to create a new object in the main method for saving input.

  5. For checking if there is the duplicate "userName" in the set of the "inputName", I loop through the objects in an array. How can I access the "userName" in this situation?

for checking duplicate, I've checked:

  1. Java - Loop through instances of a class rather than calling a method for each separate instance
  2. What is a NullPointerException, and how do I fix it?
  3. Java Array, Finding Duplicates
Woden
  • 1,054
  • 2
  • 13
  • 26
  • if you don't want duplicate nimplayers don't let them enter dupilcate names in the first place, when they enter name , you check your list of players , it that name already exists then prompt them to enter another name, and the way do are doing other it looks messy, for example and nimplayer contains a list of nimplayes is it what you want, think nimplayer as an object and give it what it needs , for gaming you can define another class called game which can have a list of players – Abhinav Chauhan Apr 17 '20 at 14:58
  • Thanks for the advice. I've already created the NimPlayer object for saving them and I also have written the NimGame class for gaming. After improving my code, it still has NullPointerException. – Woden Apr 18 '20 at 02:02

1 Answers1

1

You should address then following things in your design/code:

  1. Since you are creating a player using createPlayer(String userName, String familyName, String givenName), you should make the constructor, NimPlayer(String userName,String surName, String givenName) private so that it can not be called from outside of the class, NimPlayer. Also, declare createPlayer as static so that it doesn't need a NimPlayer object to be called on.
  2. You need to have a static counter to keep track of the number of players and check the value of this counter before adding a new player to playerList.
  3. You should also check the size of the resulting array after inputName.split(","). Similarly, you should check the size of the returned array from splitName before you access any element from it.

Given below is the code incorporating the points mentioned above:

import java.util.Scanner;

class NimPlayer {
    private String userName;
    private String familyName;
    private String givenName;
    //...
    // public getters and setters of userName, familyName, and givenName
    //...
    private static int counter = 0;
    private static NimPlayer[] playerList = new NimPlayer[10];

    private NimPlayer(String userName, String familyName, String givenName) {
        this.userName = userName;
        this.familyName = familyName;
        this.givenName = givenName;
    }

    public static void createPlayer(String userName, String familyName, String givenName) {
        if (counter < 10) {
            playerList[counter++] = new NimPlayer(userName, familyName, givenName);
        } else {
            System.out.println("The list is full.");
        }
    }

    public static int getCounter() {
        return counter;
    }

    public static NimPlayer[] getPlayers() {
        return playerList;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.print('$');
            String commandInput = in.next();
            if (commandInput.equals("addplayer")) {
                String inputName = in.nextLine();
                String[] name = splitName(inputName);
                if (name != null && name.length == 3) {
                    NimPlayer.createPlayer(name[0], name[1], name[2]);
                }
            } else {
                break;
            }
        }
        for (int i = 0; i < NimPlayer.getCounter(); i++) {
            System.out.println(NimPlayer.getPlayers()[i].getUserName());
        }
    }

    public static String[] splitName(String inputName) {
        String[] splittedLine = inputName.split(",");
        String[] name = null;
        if (splittedLine.length == 3) {
            String userName = splittedLine[0].trim();
            String familyName = splittedLine[1].trim();
            String givenName = splittedLine[2].trim();
            name = new String[3];
            name[0] = userName;
            name[1] = familyName;
            name[2] = givenName;
        }
        return name;
    }
}

I didn't understand your another question:

For checking if there is the duplicate "userName" in the set of the "inputName", I loop through the objects in an array. How can I access the "userName" in this situation?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • First, thank you very much for the clear instruction! The code seems perfect for me. About the question you mentioned, I want to check if new "userName" has already existed in the NimPlayer array that I can prompt the new users to use another "userName. – Woden Apr 18 '20 at 02:08
  • You are most welcome. Do you mean, you want to check the only `userName` and not the `familyName` and the `givenName` i.e. as long the `userName` is different (irrespective of the same `familyName` and `givenName` inputs), it is acceptable? – Arvind Kumar Avinash Apr 18 '20 at 08:41
  • yes, I figured it out though. I am now struggling to remove those players I added to the "playerList". – Woden Apr 18 '20 at 08:48
  • 1
    If your `playerList` is supposed to be dynamic (i.e. you not only want to add to it but also remove from it), I suggest you use `List playerList = new ArrayList()` instead of an array. This will make your life much easier to deal with these operations (addiing/removing elements). I am sure you will be able to do it. However, in case you get stuck with it, I suggest you post a new question so that you can keep this Q/A clean. – Arvind Kumar Avinash Apr 18 '20 at 09:00
  • For now, I use iteration and set the object to be null. Like this https://stackoverflow.com/questions/22718898/deleting-an-object-from-an-array-java#comment34622432_22718980 I'll try the advice suggested. Thanks again and have a nice day :") – Woden Apr 18 '20 at 11:08
  • Sir, I have another issue about returning the value from `NimGame` to `NimPlayer`. Could you please kindly review my code? Thanks again. Here is my new post. https://stackoverflow.com/questions/61319532/how-to-pass-values-using-setter-and-getter-among-three-classes – Woden Apr 20 '20 at 12:53