2
public static void getCount()
{
    System.out.println("How Many Players? ");
    int a = inputCount.nextInt();
    while(a <2 || a > 8)
    {
        System.out.println("How Many Players? ");
        a = inputCount.nextInt();
    }
    count = a;
}

public static void addPlayers(int count)
{ 

    Object[] playerStats = null;
    for (int i = 1;i <= count; i++)
    {

        System.out.println("Enter Name: ");
        String a = inputName.nextLine();
        name = a;
        int balance = 1500;
        int positon = 0;
        playerStats = new Object[]{i, name, balance, positon};
    }
}

In this scenario I am wanting to create a player name from the user input, I then want to add this name as well as balance and position into an object.

My issue is I am not entirely sure how I am going to both access this object outside the for loop and use them on a unique basis, as more than one will be created at a time, only with different names.

Thanks and please be gentle, I'm very new to this.

EDIT: Thanks for the responses guys, I'll try out these ideas.

2 Answers 2

2

Create a Player class to store the stats that you need, for example:

class Player {
    private final String name;
    private final int balance;
    private final int position;

    public Player(String name, int balance, int position) {
        this.name = name;
        this.balance = balance;
        this.position = position;
    }
}

And then I recommend another class to handle user input, and to create a list of players, for example:

class PlayerInput {

    private final Scanner scanner;

    PlayerInput(Scanner scanner) {
        this.scanner = scanner;
    }

    public int getCount() {
        int count;
        do {
            System.out.println("How Many Players? ");
            count = scanner.nextInt();
        } while (count < 2 || count > 8);
        return count;
    }

    public List<Player> inputPlayers(int count) {
        List<Player> players = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            System.out.println("Enter Name: ");
            String name = scanner.nextLine();
            int balance = 1500;
            int position = 0;
            players.add(new Player(name, balance, position));
        }
        return players;
    }
}

You could use these classes for example like this:

public class App {
    public static void main(String[] args) {
        PlayerInput input = new PlayerInput(new Scanner(System.in));
        int count = input.getCount();
        List<Player> players = input.inputPlayers(count);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

perhaps I been looking at this problem for too long, but how would I call the getCount method in main?
I added an example to my answer, I hope it helps. These things are just examples though, to get you started, they are not something complete
0
Object[] playerStats = new Object[count];
    for (int i = 1;i <= count; i++)
    {

        System.out.println("Enter Name: ");
        String a = inputName.nextLine();
        name = a;
        int balance = 1500;
        int positon = 0;
        playerStats[i] = new Object(i, name, balance, positon);
    }

This is what you should be doing when having an array of different objects. To acces, let's say, the name of index 1 of the array :

playerStats[i].getName();

But note that your array is only accessible inside of your method.

Why not change the return type to Object[] and return the created array. Like that, it would be accessible anywhere else.

Comments

Your Answer

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.