0

I am new to Java and this is a very basic question. However I struggle to find a solution, so hopefully someone could give me some pointers.

I am trying to fill values into an array "addedPlayer". However, every time I run the AddPlayer() method it is initialiezed to zero again.

How can I structure this in a better way?

public class DemoApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public void AddPlayer() {
        int[] addedPlayer;

        addedPlayer = new int[500];

        System.out.println(" *** Add new player *** ");
        System.out.println("Name:");
        String name = System.console().readLine();
        System.out.println("Age:");
        int age = Integer.parseInt(System.console().readLine());
        System.out.println("JNUM:");
        int jnum = Integer.parseInt(System.console().readLine());

        player p = new player();
        p.SetAge(age);
        p.SetName(name);
        p.SetJnum(jnum);

        System.out.println(addedPlayer[0]);
        for (int j = 0; j < addedPlayer.length; j++) {
            if (addedPlayer[j] != 0) {
            } else {
                addedPlayer[j] = p.GetAge();
                System.out.println(addedPlayer[j]);
                System.out.println(j);
                break;
            }
        }
    }

    public void EditPlayer() {
        //empty
    }

    public void ListPlayer() {
        //empty
    }

    @Override
    public void run(String... args) throws Exception {
        while (true) {
            System.out.println(" *** MENY *** ");
            System.out.println(" 1. Add player ");
            System.out.println(" 2. Edit player ");//ÖKurs
            System.out.println(" 3. List player ");
            System.out.println(" 100. Exit ");
            System.out.println("Ange val");
            int sel = Integer.parseInt(System.console().readLine());

            if (sel == 100) break;
            if (sel == 1) AddPlayer();
            if (sel == 2) EditPlayer();
            if (sel == 3) AddPlayer();
        }
    }
}
2
  • Perhaps a static class variable is needed instead of a local variable in the method! Commented Feb 23, 2021 at 21:00
  • addedPlayer array needs to be defined outside the method. Currently is it being allocated on the stack and disappers when the method is completed. Declare it at class level Commented Feb 23, 2021 at 21:12

3 Answers 3

1

Each time you run AddPlayer(), it creates a new player from scratch. If you want to keep your modifications to a bare minimum, you must put it outside of the method and make it a property for your class like List<int[]> addedPlayers = new ArrayList<int[]>(); and you can add this line AddPlayer to add it in a list addedPlayers.add(addedPlayer). Otherwise, if you want a more cleaner code, you should add more classes than only one main class. To improve your code, you can see @g.momo's answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I added a list like you mentioned and this solved my problem. But agree that more classes like suggested by @g.momo would be cleaner.
1

int[] addedPlayer; addedPlayer = new int[500]; It gets overridden because you are creating an new local var addedPlayer, and then setting all values to 0 (addedPlayer = new int[500];) I'm assuming you would want addedPlayer to be global, so don't define it locally and set it to 0.

Also, should addedPlayer be a player[] or just a player rather than an int[]? Plus, you didn't close the function in the code you gave us, so is there more missing or did you just not close it?

Comments

0

Your code and your expectations are completely differents.

Read this and tell us if you understand. It is the way I would have written if I were you . But it is NOT TESTED:

public class AddPlayer { // you create a class

    player[] addedPlayer; // array of players
    int index;
    
    public AddPlayer() { // constructor of the class
        addedPlayer = new player[500]; // max 500 players
        index = 0;
    }

    public void addPlayer(player p) {
        if(index < 500) {
            addedPlayer[index] = p; // add at index, 
            index = index + 1; // then increment index for the next added player
        }
    }

    public static void main(String... args) {
        AddPlayer addPlayers = new AddPlayer();
       
        int i = 0;
        while(i < 5) { // will run 5 times, so only 5 players will be added. Change to stop when you will need
            
            // here your read console inputs
            System.out.println(" *** Add new player *** "+ (i+1));
            System.out.println("Name:");
            String name = System.console().readLine();
            System.out.println("Age:");
            int age = Integer.parseInt(System.console().readLine());
            System.out.println("JNUM:");
            int jnum = Integer.parseInt(System.console().readLine());
            
            // initialize the player
            player p = new player();
            p.SetAge(age);
            p.SetName(name);
            p.SetJnum(jnum);
 
            addPlayers.addPlayer(p); // add in the array

            i++;
        }
    }
}


1 Comment

What I want to do is actually just to add age,name and shirt number of new players from a user input into an array. I understand your code and will test tomorrow evening. THANKS!

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.