'How can you use a passed value from one class to another in a for loop?

So I have a user field that they can type in 1-10 for number of players:

playersFld.setTextFormatter(new TextFormatter<String>(playersValidator));

custartBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, actionEvent4 -> {

Integer playersNum = Integer.valueOf(playersFld.getText());
if (!playersNum.equals("")) {
                
                System.out.println("Got the values");
                try {
                    FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/score_page.fxml"));
                    root = loader.load();
                    ScorePageCtrl scorePageCtrl = loader.getController();

                    scorePageCtrl.displayDate(strDate);
                    scorePageCtrl.recievePlayers(playersNum);

                    stage = (Stage) ((Node) actionEvent4.getSource()).getScene().getWindow();
                    scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();

                } catch (IOException e) {
                    e.printStackTrace();
                }


            } else {
                System.out.println("missing value(s)");
            }
});

I pass that to the next page's controller in this method:

public int recievePlayers(int plrNum){
    System.out.println("Players: " + plrNum);
    return plrNum;
}

The sout let's me know I'm getting the correct number, but I can't seem to pass the returned value to a for array

AtomicInteger tabs = new AtomicInteger(2);
    for (int i = 2; i <= recievePlayers(); i++) {
        if (tabs.get() <= 10) {
            tabPaneSP.getTabs().add(new Tab("Player" + tabs.getAndIncrement()));
            tabPaneSP.getSelectionModel().selectLast();
        } else {
            System.out.println("No more homies");
        }
    }

I've tried the method name, the integer name, making int p; and then attaching it to the return, but nothing seems to work.

EDIT: So I've tried changing things and I can get it to work but for some reason it only fires every other time, which is less than ideal.

I changed:

public static int plrNum;

public int receivePlayers(int players) {
    System.out.println("Players: " + players);
    //Used to make sure I get the number datesSP.setText(String.valueOf(players));
    return this.plrNum = players;
}

And use this to instantiate so I can put it in the for loop:

int n = plrNum;

But there has to be a better way.

EDIT: So I've updated my gist to reflect the current code that is working, but for some reason it only works every other time.

https://gist.github.com/Spider-Ian/3d5c777171d7ad632e9b71943fcf950c



Solution 1:[1]

Okay apparently there is a very strict course of events that happen in a controller. It loads FXML items, it initializes and then it looks for other methods and fun things to play with. So, the way to make sure that the tabs get the data first, I can call them ahead of time with the previous controller.

threeThirBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, actionEvent1 -> {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/score_page.fxml"));
            root = loader.load();
            ScorePageCtrl scorePageCtrl = loader.getController();

            scorePageCtrl.displayDate(strDate);
            scorePageCtrl.displayEvent("3 x 30");
            scorePageCtrl.receivePlayers(3);
            scorePageCtrl.receivePlays(3);
            scorePageCtrl.receiveRounds(30);
//**Here is where I told the method to get the returned value before initialization**
            scorePageCtrl.addTabs();
            scorePageCtrl.addRows();

            stage = (Stage) ((Node) actionEvent1.getSource()).getScene().getWindow();
            scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }

        //TODO: Set the row and column numbers to thirty rows and three columns

        System.out.println("3 x 30 button clicked");

    });

I did have to move the AtomicInteger out of the initialize and into its own method.

public void addTabs() {
    int plr = plrNum;

    AtomicInteger tabs = new AtomicInteger(2);
    for (int i = 2; i <= plr; i++) {
        if (tabs.get() <= 10) {
            tabPaneSP.getTabs().add(new Tab("Player" + tabs.getAndIncrement()));
            tabPaneSP.getSelectionModel().selectLast();
        } else {
            System.out.println("No more homies");
        }
    }
    addPlayerBtn.setOnAction(event -> {
        if (tabs.get() <= 10) {
            tabPaneSP.getTabs().add(new Tab("Player" + tabs.getAndIncrement()));
            tabPaneSP.getSelectionModel().selectLast();
        } else {
            System.out.println("No more homies");
        }
    });
}

The only think I'm still not happy with is the amount of hoops to get a get value to return as an useable integer.

Solution 2:[2]

Your method takes a parameter, but you are calling it without. Change it to

public int recievePlayers() {

and it should work.

Bonus tip! Change it to

public int receivePlayers() {

to make it easier to use.

Solution 3:[3]

For example, this way can work also.

public class Player {

    public int num;

    public int recievePlayers(int plrNum){
        System.out.println("Players: " + plrNum);
        return this.num = plrNum;
    }

}
    
    
    
  public static void main(String[] args) {
        
        
        Player player = new Player();
        player.recievePlayers(3);
        
        int n = player.num;
        
        AtomicInteger tabs = new AtomicInteger(2);
        for (int i = 2; i <= n; i++) {
            if (tabs.get() <= 10) {
                tabPaneSP.getTabs().add(new Tab("Player" + tabs.getAndIncrement()));
                tabPaneSP.getSelectionModel().selectLast();
                
            } else {
                System.out.println("No more homies");
            }
        }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 horribly_n00bie
Solution 2 Sam
Solution 3 prostý ?lov?k