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

Time:04-06

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.

CodePudding user response:

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.

CodePudding user response:

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");
            }
        }

}

  • Related