JavaFX - Need a for loop to pause and wait for button input











up vote
0
down vote

favorite












My questions has been answered and my problems solved. I've updated this thread with my final solution



I'm working on this school assignment (quiz program) which we were initially only required to do as a pure text-based console game, but I wanted to challenge myself and try to create it in JavaFX which we have barely begun learning about.



It's a turn-based quiz game, where the player will be presented with a question which s/he will be required to answer either True/False to through two buttons (True or False buttons)



Game flow:



I'm running a for-loop which continues as long as there are questions left in my ArrayList.



The ideal flow would go like this:



0) Enter names and press: Start Game button
1) Present question to player 1.
2) Receive an input through either the True button or the False button.
3) Compare the input with the correct answer pulled from the array.
4) If correct, user gets 1 point - If false, continue to player 2.
5) Change to player two and start over again until no more questions in the ArrayList.



What's working
I've got my loop to "function correctly" in the sense that it changes player and continues until there are no more questions in the ArrayList.



Problem
I want the for loop to wait for user input through the true/false buttons.
However, the for loop runs the entire program without waiting for input.



Question
Is it possible to "pause" a for loop and have it wait for an input from one of two buttons?
Are there other ways you'd recommend I'd handle this problem?



I'd be super happy if you'd be willing to direct on solving this. Whether through this forum or by sending me a link to further reading is ok. I just can't seem to find a solution by googling it or searching this forum.



The code is filled with System.out.println() for me to see how far the program runs. Please look away from this.



Also... Any suggestions on how I could improve the code would be highly appreciated. Only 8 weeks into Java programming and would love to learn how to do things smarter.



The Main code



package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Expert Quiz Game");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}


public static void main(String args) {
launch(args);
}
}


The Controller code



package sample;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import java.util.Random;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text questionText;
@FXML
Text questionsLeft;
@FXML
Text gameStatus;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
//@FXML
//Button nextRound;

@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

String playerOneName = player1.getText(); //Saves the name for later use
String playerTwoName = player2.getText(); //Saves the name for later use
int playerOneScore = 0;
int playerTwoScore = 0;
Random randomQuestionNumber1 = new Random();
//Random randomQuestionNumber2 = new Random();

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

ArrayList<String> questionsArray = new ArrayList<String>();
questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

ArrayList<String> answerArray = new ArrayList<String>();
answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

//int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size()); //+0
//int randomQuestionB = randomQuestionNumber2.nextInt(questionsArray.size()); //+0

//Button clickedButton = (Button) event.getTarget();
//String buttonLabel = clickedButton.getText();
int player = 1;

for (int i = 0; i < questionsArray.size(); i++) {

if (player == 1) {

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******



player = 2;
System.out.println("Changing to player " +player);
} else if (player == 2) {
//clickedButton.setText("O");

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******

player = 1;
System.out.println("Changing to player " +player);

}
}
}
@FXML
private String handleButtonAnswer(ActionEvent event) {
String buttonAnswer = "";
// (1)
Button b = (Button) event.getSource();
// (2)
String t = b.getText();

System.out.println("4. the text on the button was: "+t);

if (t.equals("True")){
buttonAnswer = "True"; // demo
}else if (t.equals("False")){
buttonAnswer = "False";
}
return buttonAnswer;
}


}



The fxml code



Design is not done - Need it to work before styling it properly



<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="794.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="225.0" layoutY="47.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player2" />
<Text fx:id="score1" layoutX="79.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Ponits" />
<Text fx:id="score2" layoutX="503.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Points" />
<Text fx:id="questionText" layoutX="38.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="318.0" layoutY="154.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="301.0" layoutY="505.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625" />
<Text fx:id="gameStatus" layoutX="320.0" layoutY="547.0" strokeType="OUTSIDE" strokeWidth="0.0" text="The winner is:" />
<Button fx:id="buttonTrue" layoutX="150.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" layoutX="582.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />

</children>
</AnchorPane>


EVERYTHING IS WORKING - HERE'S MY FINAL SOLUTION



package sample;

import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text statusText;
@FXML
Text questionsLeft;
@FXML
Text currentPlayer;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
@FXML
Button nextRound;

private ArrayList<String> questionsArray = new ArrayList<String>();

private ArrayList<String> answerArray = new ArrayList<String>();

private int player = 0;

private int remainingQuestions = 0;

private int playerOneScore;

private int playerTwoScore;

private String playerOneName = "";

private String playerTwoName = "";

private String answerFromArray = "";

private String answerButtonText = "";

private Random randomQuestionNumber = new Random();

private int randomQuestion = 0;


@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

playerOneName = player1.getText(); //Saves the name for later use
playerTwoName = player2.getText(); //Saves the name for later use

score1.setText(playerOneName+ " has: 0 points;");
score2.setText(playerTwoName+ " has: 0 points;");

playerOneScore = 0;
playerTwoScore = 0;

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

// ******* THIS IS THE PLAYER STARTING THE ROUND ******

player = 1;

// ****** RETRIEVE AND PRESENT FIRST QUESTION - THIS WILL ONLY BE USED TO START THE PROGRAM ******
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size()); // GENERATES A RANDOM NUMBER TO PICK A RANDOM QUESTION FROM ARRAYLIST
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS THE RANDOM QUESTION TO THE UI

currentPlayer.setText("Current Player: "+playerOneName);

buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
startGame.setDisable(true);
}

@FXML
private void handleButtonAnswer(ActionEvent event) {
Button answerButtonClicked = (Button) event.getSource();

answerButtonText = answerButtonClicked.getText();
answerFromArray = answerArray.get(randomQuestion); // PICKS THE ANSWER MATCHING THE QUESTION GENERATED BY THE RANDOM NUMBER

if (answerFromArray.equals(answerButtonText)) {
statusText.setText("Answer was correct! You've been awarded 1 point");
if(player == 1){
playerOneScore = playerOneScore+1;
score1.setText(playerOneName+ " has: "+playerOneScore+" points;");
}else if(player == 2){
playerTwoScore = playerTwoScore+1;
score2.setText(playerTwoName+ " has: "+playerTwoScore+" points;");
}
}else{
statusText.setText("Answer was incorrect! No point was given.");
}
nextRound.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}

@FXML
private void handleButtonNext(ActionEvent event) {
if(player == 1){
player = 2;
currentPlayer.setText("Current Player: "+playerTwoName);
}else if(player == 2){
player = 1;
currentPlayer.setText("Current Player: "+playerOneName);
}
questionsArray.remove(randomQuestion); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestion); // REMOVES ANSWER FROM ARRAYLIST
remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
if(questionsArray.size()!=0){
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size());
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS QUESTION TO THE UI
}else{
if(playerOneScore>playerTwoScore){
questionsLeft.setText("Game is over. The winner is: "+playerOneName);
}else if(playerTwoScore>playerOneScore){
questionsLeft.setText("Game is over. The winner is: "+playerTwoName);
}else{
questionsLeft.setText("The game ended in a DRAW. ");
}
startGame.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}
nextRound.setDisable(true);
buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
}
}


fxml



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>


<Label layoutX="233.0" layoutY="46.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player 1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player 2" />
<Text fx:id="score1" layoutX="114.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 has: 0 points" />
<Text fx:id="score2" layoutX="558.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 has: 0 points" />
<Text fx:id="statusText" layoutX="41.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="363.0" layoutY="161.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="304.0" layoutY="473.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625">
<font>
<Font size="18.0" />
</font></Text>
<Button fx:id="buttonTrue" disable="true" layoutX="150.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" disable="true" layoutX="592.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />
<Button fx:id="nextRound" disable="true" layoutX="354.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonNext" text="Next Question" />
<Text fx:id="currentPlayer" layoutX="355.0" layoutY="260.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current player:">
<font>
<Font size="14.0" />
</font>
</Text>
</children>











share|improve this question




















  • 1




    Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
    – jewelsea
    Nov 19 at 19:01

















up vote
0
down vote

favorite












My questions has been answered and my problems solved. I've updated this thread with my final solution



I'm working on this school assignment (quiz program) which we were initially only required to do as a pure text-based console game, but I wanted to challenge myself and try to create it in JavaFX which we have barely begun learning about.



It's a turn-based quiz game, where the player will be presented with a question which s/he will be required to answer either True/False to through two buttons (True or False buttons)



Game flow:



I'm running a for-loop which continues as long as there are questions left in my ArrayList.



The ideal flow would go like this:



0) Enter names and press: Start Game button
1) Present question to player 1.
2) Receive an input through either the True button or the False button.
3) Compare the input with the correct answer pulled from the array.
4) If correct, user gets 1 point - If false, continue to player 2.
5) Change to player two and start over again until no more questions in the ArrayList.



What's working
I've got my loop to "function correctly" in the sense that it changes player and continues until there are no more questions in the ArrayList.



Problem
I want the for loop to wait for user input through the true/false buttons.
However, the for loop runs the entire program without waiting for input.



Question
Is it possible to "pause" a for loop and have it wait for an input from one of two buttons?
Are there other ways you'd recommend I'd handle this problem?



I'd be super happy if you'd be willing to direct on solving this. Whether through this forum or by sending me a link to further reading is ok. I just can't seem to find a solution by googling it or searching this forum.



The code is filled with System.out.println() for me to see how far the program runs. Please look away from this.



Also... Any suggestions on how I could improve the code would be highly appreciated. Only 8 weeks into Java programming and would love to learn how to do things smarter.



The Main code



package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Expert Quiz Game");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}


public static void main(String args) {
launch(args);
}
}


The Controller code



package sample;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import java.util.Random;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text questionText;
@FXML
Text questionsLeft;
@FXML
Text gameStatus;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
//@FXML
//Button nextRound;

@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

String playerOneName = player1.getText(); //Saves the name for later use
String playerTwoName = player2.getText(); //Saves the name for later use
int playerOneScore = 0;
int playerTwoScore = 0;
Random randomQuestionNumber1 = new Random();
//Random randomQuestionNumber2 = new Random();

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

ArrayList<String> questionsArray = new ArrayList<String>();
questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

ArrayList<String> answerArray = new ArrayList<String>();
answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

//int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size()); //+0
//int randomQuestionB = randomQuestionNumber2.nextInt(questionsArray.size()); //+0

//Button clickedButton = (Button) event.getTarget();
//String buttonLabel = clickedButton.getText();
int player = 1;

for (int i = 0; i < questionsArray.size(); i++) {

if (player == 1) {

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******



player = 2;
System.out.println("Changing to player " +player);
} else if (player == 2) {
//clickedButton.setText("O");

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******

player = 1;
System.out.println("Changing to player " +player);

}
}
}
@FXML
private String handleButtonAnswer(ActionEvent event) {
String buttonAnswer = "";
// (1)
Button b = (Button) event.getSource();
// (2)
String t = b.getText();

System.out.println("4. the text on the button was: "+t);

if (t.equals("True")){
buttonAnswer = "True"; // demo
}else if (t.equals("False")){
buttonAnswer = "False";
}
return buttonAnswer;
}


}



The fxml code



Design is not done - Need it to work before styling it properly



<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="794.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="225.0" layoutY="47.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player2" />
<Text fx:id="score1" layoutX="79.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Ponits" />
<Text fx:id="score2" layoutX="503.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Points" />
<Text fx:id="questionText" layoutX="38.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="318.0" layoutY="154.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="301.0" layoutY="505.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625" />
<Text fx:id="gameStatus" layoutX="320.0" layoutY="547.0" strokeType="OUTSIDE" strokeWidth="0.0" text="The winner is:" />
<Button fx:id="buttonTrue" layoutX="150.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" layoutX="582.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />

</children>
</AnchorPane>


EVERYTHING IS WORKING - HERE'S MY FINAL SOLUTION



package sample;

import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text statusText;
@FXML
Text questionsLeft;
@FXML
Text currentPlayer;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
@FXML
Button nextRound;

private ArrayList<String> questionsArray = new ArrayList<String>();

private ArrayList<String> answerArray = new ArrayList<String>();

private int player = 0;

private int remainingQuestions = 0;

private int playerOneScore;

private int playerTwoScore;

private String playerOneName = "";

private String playerTwoName = "";

private String answerFromArray = "";

private String answerButtonText = "";

private Random randomQuestionNumber = new Random();

private int randomQuestion = 0;


@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

playerOneName = player1.getText(); //Saves the name for later use
playerTwoName = player2.getText(); //Saves the name for later use

score1.setText(playerOneName+ " has: 0 points;");
score2.setText(playerTwoName+ " has: 0 points;");

playerOneScore = 0;
playerTwoScore = 0;

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

// ******* THIS IS THE PLAYER STARTING THE ROUND ******

player = 1;

// ****** RETRIEVE AND PRESENT FIRST QUESTION - THIS WILL ONLY BE USED TO START THE PROGRAM ******
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size()); // GENERATES A RANDOM NUMBER TO PICK A RANDOM QUESTION FROM ARRAYLIST
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS THE RANDOM QUESTION TO THE UI

currentPlayer.setText("Current Player: "+playerOneName);

buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
startGame.setDisable(true);
}

@FXML
private void handleButtonAnswer(ActionEvent event) {
Button answerButtonClicked = (Button) event.getSource();

answerButtonText = answerButtonClicked.getText();
answerFromArray = answerArray.get(randomQuestion); // PICKS THE ANSWER MATCHING THE QUESTION GENERATED BY THE RANDOM NUMBER

if (answerFromArray.equals(answerButtonText)) {
statusText.setText("Answer was correct! You've been awarded 1 point");
if(player == 1){
playerOneScore = playerOneScore+1;
score1.setText(playerOneName+ " has: "+playerOneScore+" points;");
}else if(player == 2){
playerTwoScore = playerTwoScore+1;
score2.setText(playerTwoName+ " has: "+playerTwoScore+" points;");
}
}else{
statusText.setText("Answer was incorrect! No point was given.");
}
nextRound.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}

@FXML
private void handleButtonNext(ActionEvent event) {
if(player == 1){
player = 2;
currentPlayer.setText("Current Player: "+playerTwoName);
}else if(player == 2){
player = 1;
currentPlayer.setText("Current Player: "+playerOneName);
}
questionsArray.remove(randomQuestion); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestion); // REMOVES ANSWER FROM ARRAYLIST
remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
if(questionsArray.size()!=0){
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size());
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS QUESTION TO THE UI
}else{
if(playerOneScore>playerTwoScore){
questionsLeft.setText("Game is over. The winner is: "+playerOneName);
}else if(playerTwoScore>playerOneScore){
questionsLeft.setText("Game is over. The winner is: "+playerTwoName);
}else{
questionsLeft.setText("The game ended in a DRAW. ");
}
startGame.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}
nextRound.setDisable(true);
buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
}
}


fxml



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>


<Label layoutX="233.0" layoutY="46.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player 1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player 2" />
<Text fx:id="score1" layoutX="114.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 has: 0 points" />
<Text fx:id="score2" layoutX="558.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 has: 0 points" />
<Text fx:id="statusText" layoutX="41.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="363.0" layoutY="161.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="304.0" layoutY="473.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625">
<font>
<Font size="18.0" />
</font></Text>
<Button fx:id="buttonTrue" disable="true" layoutX="150.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" disable="true" layoutX="592.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />
<Button fx:id="nextRound" disable="true" layoutX="354.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonNext" text="Next Question" />
<Text fx:id="currentPlayer" layoutX="355.0" layoutY="260.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current player:">
<font>
<Font size="14.0" />
</font>
</Text>
</children>











share|improve this question




















  • 1




    Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
    – jewelsea
    Nov 19 at 19:01















up vote
0
down vote

favorite









up vote
0
down vote

favorite











My questions has been answered and my problems solved. I've updated this thread with my final solution



I'm working on this school assignment (quiz program) which we were initially only required to do as a pure text-based console game, but I wanted to challenge myself and try to create it in JavaFX which we have barely begun learning about.



It's a turn-based quiz game, where the player will be presented with a question which s/he will be required to answer either True/False to through two buttons (True or False buttons)



Game flow:



I'm running a for-loop which continues as long as there are questions left in my ArrayList.



The ideal flow would go like this:



0) Enter names and press: Start Game button
1) Present question to player 1.
2) Receive an input through either the True button or the False button.
3) Compare the input with the correct answer pulled from the array.
4) If correct, user gets 1 point - If false, continue to player 2.
5) Change to player two and start over again until no more questions in the ArrayList.



What's working
I've got my loop to "function correctly" in the sense that it changes player and continues until there are no more questions in the ArrayList.



Problem
I want the for loop to wait for user input through the true/false buttons.
However, the for loop runs the entire program without waiting for input.



Question
Is it possible to "pause" a for loop and have it wait for an input from one of two buttons?
Are there other ways you'd recommend I'd handle this problem?



I'd be super happy if you'd be willing to direct on solving this. Whether through this forum or by sending me a link to further reading is ok. I just can't seem to find a solution by googling it or searching this forum.



The code is filled with System.out.println() for me to see how far the program runs. Please look away from this.



Also... Any suggestions on how I could improve the code would be highly appreciated. Only 8 weeks into Java programming and would love to learn how to do things smarter.



The Main code



package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Expert Quiz Game");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}


public static void main(String args) {
launch(args);
}
}


The Controller code



package sample;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import java.util.Random;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text questionText;
@FXML
Text questionsLeft;
@FXML
Text gameStatus;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
//@FXML
//Button nextRound;

@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

String playerOneName = player1.getText(); //Saves the name for later use
String playerTwoName = player2.getText(); //Saves the name for later use
int playerOneScore = 0;
int playerTwoScore = 0;
Random randomQuestionNumber1 = new Random();
//Random randomQuestionNumber2 = new Random();

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

ArrayList<String> questionsArray = new ArrayList<String>();
questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

ArrayList<String> answerArray = new ArrayList<String>();
answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

//int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size()); //+0
//int randomQuestionB = randomQuestionNumber2.nextInt(questionsArray.size()); //+0

//Button clickedButton = (Button) event.getTarget();
//String buttonLabel = clickedButton.getText();
int player = 1;

for (int i = 0; i < questionsArray.size(); i++) {

if (player == 1) {

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******



player = 2;
System.out.println("Changing to player " +player);
} else if (player == 2) {
//clickedButton.setText("O");

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******

player = 1;
System.out.println("Changing to player " +player);

}
}
}
@FXML
private String handleButtonAnswer(ActionEvent event) {
String buttonAnswer = "";
// (1)
Button b = (Button) event.getSource();
// (2)
String t = b.getText();

System.out.println("4. the text on the button was: "+t);

if (t.equals("True")){
buttonAnswer = "True"; // demo
}else if (t.equals("False")){
buttonAnswer = "False";
}
return buttonAnswer;
}


}



The fxml code



Design is not done - Need it to work before styling it properly



<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="794.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="225.0" layoutY="47.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player2" />
<Text fx:id="score1" layoutX="79.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Ponits" />
<Text fx:id="score2" layoutX="503.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Points" />
<Text fx:id="questionText" layoutX="38.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="318.0" layoutY="154.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="301.0" layoutY="505.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625" />
<Text fx:id="gameStatus" layoutX="320.0" layoutY="547.0" strokeType="OUTSIDE" strokeWidth="0.0" text="The winner is:" />
<Button fx:id="buttonTrue" layoutX="150.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" layoutX="582.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />

</children>
</AnchorPane>


EVERYTHING IS WORKING - HERE'S MY FINAL SOLUTION



package sample;

import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text statusText;
@FXML
Text questionsLeft;
@FXML
Text currentPlayer;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
@FXML
Button nextRound;

private ArrayList<String> questionsArray = new ArrayList<String>();

private ArrayList<String> answerArray = new ArrayList<String>();

private int player = 0;

private int remainingQuestions = 0;

private int playerOneScore;

private int playerTwoScore;

private String playerOneName = "";

private String playerTwoName = "";

private String answerFromArray = "";

private String answerButtonText = "";

private Random randomQuestionNumber = new Random();

private int randomQuestion = 0;


@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

playerOneName = player1.getText(); //Saves the name for later use
playerTwoName = player2.getText(); //Saves the name for later use

score1.setText(playerOneName+ " has: 0 points;");
score2.setText(playerTwoName+ " has: 0 points;");

playerOneScore = 0;
playerTwoScore = 0;

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

// ******* THIS IS THE PLAYER STARTING THE ROUND ******

player = 1;

// ****** RETRIEVE AND PRESENT FIRST QUESTION - THIS WILL ONLY BE USED TO START THE PROGRAM ******
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size()); // GENERATES A RANDOM NUMBER TO PICK A RANDOM QUESTION FROM ARRAYLIST
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS THE RANDOM QUESTION TO THE UI

currentPlayer.setText("Current Player: "+playerOneName);

buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
startGame.setDisable(true);
}

@FXML
private void handleButtonAnswer(ActionEvent event) {
Button answerButtonClicked = (Button) event.getSource();

answerButtonText = answerButtonClicked.getText();
answerFromArray = answerArray.get(randomQuestion); // PICKS THE ANSWER MATCHING THE QUESTION GENERATED BY THE RANDOM NUMBER

if (answerFromArray.equals(answerButtonText)) {
statusText.setText("Answer was correct! You've been awarded 1 point");
if(player == 1){
playerOneScore = playerOneScore+1;
score1.setText(playerOneName+ " has: "+playerOneScore+" points;");
}else if(player == 2){
playerTwoScore = playerTwoScore+1;
score2.setText(playerTwoName+ " has: "+playerTwoScore+" points;");
}
}else{
statusText.setText("Answer was incorrect! No point was given.");
}
nextRound.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}

@FXML
private void handleButtonNext(ActionEvent event) {
if(player == 1){
player = 2;
currentPlayer.setText("Current Player: "+playerTwoName);
}else if(player == 2){
player = 1;
currentPlayer.setText("Current Player: "+playerOneName);
}
questionsArray.remove(randomQuestion); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestion); // REMOVES ANSWER FROM ARRAYLIST
remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
if(questionsArray.size()!=0){
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size());
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS QUESTION TO THE UI
}else{
if(playerOneScore>playerTwoScore){
questionsLeft.setText("Game is over. The winner is: "+playerOneName);
}else if(playerTwoScore>playerOneScore){
questionsLeft.setText("Game is over. The winner is: "+playerTwoName);
}else{
questionsLeft.setText("The game ended in a DRAW. ");
}
startGame.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}
nextRound.setDisable(true);
buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
}
}


fxml



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>


<Label layoutX="233.0" layoutY="46.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player 1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player 2" />
<Text fx:id="score1" layoutX="114.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 has: 0 points" />
<Text fx:id="score2" layoutX="558.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 has: 0 points" />
<Text fx:id="statusText" layoutX="41.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="363.0" layoutY="161.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="304.0" layoutY="473.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625">
<font>
<Font size="18.0" />
</font></Text>
<Button fx:id="buttonTrue" disable="true" layoutX="150.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" disable="true" layoutX="592.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />
<Button fx:id="nextRound" disable="true" layoutX="354.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonNext" text="Next Question" />
<Text fx:id="currentPlayer" layoutX="355.0" layoutY="260.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current player:">
<font>
<Font size="14.0" />
</font>
</Text>
</children>











share|improve this question















My questions has been answered and my problems solved. I've updated this thread with my final solution



I'm working on this school assignment (quiz program) which we were initially only required to do as a pure text-based console game, but I wanted to challenge myself and try to create it in JavaFX which we have barely begun learning about.



It's a turn-based quiz game, where the player will be presented with a question which s/he will be required to answer either True/False to through two buttons (True or False buttons)



Game flow:



I'm running a for-loop which continues as long as there are questions left in my ArrayList.



The ideal flow would go like this:



0) Enter names and press: Start Game button
1) Present question to player 1.
2) Receive an input through either the True button or the False button.
3) Compare the input with the correct answer pulled from the array.
4) If correct, user gets 1 point - If false, continue to player 2.
5) Change to player two and start over again until no more questions in the ArrayList.



What's working
I've got my loop to "function correctly" in the sense that it changes player and continues until there are no more questions in the ArrayList.



Problem
I want the for loop to wait for user input through the true/false buttons.
However, the for loop runs the entire program without waiting for input.



Question
Is it possible to "pause" a for loop and have it wait for an input from one of two buttons?
Are there other ways you'd recommend I'd handle this problem?



I'd be super happy if you'd be willing to direct on solving this. Whether through this forum or by sending me a link to further reading is ok. I just can't seem to find a solution by googling it or searching this forum.



The code is filled with System.out.println() for me to see how far the program runs. Please look away from this.



Also... Any suggestions on how I could improve the code would be highly appreciated. Only 8 weeks into Java programming and would love to learn how to do things smarter.



The Main code



package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Expert Quiz Game");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}


public static void main(String args) {
launch(args);
}
}


The Controller code



package sample;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import java.util.Random;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text questionText;
@FXML
Text questionsLeft;
@FXML
Text gameStatus;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
//@FXML
//Button nextRound;

@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

String playerOneName = player1.getText(); //Saves the name for later use
String playerTwoName = player2.getText(); //Saves the name for later use
int playerOneScore = 0;
int playerTwoScore = 0;
Random randomQuestionNumber1 = new Random();
//Random randomQuestionNumber2 = new Random();

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

ArrayList<String> questionsArray = new ArrayList<String>();
questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

ArrayList<String> answerArray = new ArrayList<String>();
answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

//int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size()); //+0
//int randomQuestionB = randomQuestionNumber2.nextInt(questionsArray.size()); //+0

//Button clickedButton = (Button) event.getTarget();
//String buttonLabel = clickedButton.getText();
int player = 1;

for (int i = 0; i < questionsArray.size(); i++) {

if (player == 1) {

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******



player = 2;
System.out.println("Changing to player " +player);
} else if (player == 2) {
//clickedButton.setText("O");

int randomQuestionA = randomQuestionNumber1.nextInt(questionsArray.size());

// ****** RETRIEVE AND PRESENT FIRST QUESTION ******

System.out.println("1. Player: " + playerOneName); // WRITE TO CONSOLE THE CURRENT PLAYER * CAN BE REMOVED
questionText.setText("Question: " + questionsArray.get(randomQuestionA)); // PRINTS QUESTION TO THE UI
System.out.println("2. "+questionsArray.get(randomQuestionA)); // WRITES TO CONSOLE THE QUESTION * CAN BE REMOVED

// ****** GET ANSWER FOR THE QUESTION ******

String answerFromArray = answerArray.get(randomQuestionA); // TAKES THE ANSWER FROM THE ARRAY AND SAVES AS A NEW STRING
System.out.println("3." + answerArray.get(randomQuestionA)); // WRITES THE ANSWER FROM THE ARRAY TO CONSOLE * CAN BE REMOVED

// ****** TEST IF ANSWER IS TRUE ******

String buttonTrueText = buttonTrue.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON
String buttonFalseText = buttonFalse.getText(); // SAVES THE BUTTON TEXT AS A STRING FOR LATER COMPARISON

// ****** IF TRUE, THEN COUNT ONE UP IN SCORE ******

buttonTrue.setOnAction(e -> {
System.out.println("4. Now running"); // WRITES TO CONSOLE * CAN BE REMOVED

String buttonAnswer = " "; // INITIALIZES THE VARIABLE WITH AN EMPTY STRING
handleButtonAnswer(event);
/**if (buttonText.equals("True")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "True";
} else if (buttonText.equals("False")) { // REGISTERS THE BUTTON PRESSED AND SAVES IT IN A VARIABLE
buttonAnswer = "False";
}**/
if (buttonAnswer == "True" || buttonAnswer == "False") {
System.out.println("5. Test");
}


System.out.println("6. "+buttonAnswer);
if (answerFromArray.equals(buttonAnswer)) {
System.out.println("7. Answer was correct - Need to count one up in score");//
}

score1.setText("Points: " + playerOneScore+1);
System.out.println("8. "+playerOneScore+1);
questionsArray.remove(randomQuestionA); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestionA); // REMOVES ANSWER FROM ARRAYLIST

int remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
});

// ****** REMOVE THAT QUESTION + ANSWER FROM THE ARRAY ******

// ****** CHANGE PLAYER AND START AGAIN ******

player = 1;
System.out.println("Changing to player " +player);

}
}
}
@FXML
private String handleButtonAnswer(ActionEvent event) {
String buttonAnswer = "";
// (1)
Button b = (Button) event.getSource();
// (2)
String t = b.getText();

System.out.println("4. the text on the button was: "+t);

if (t.equals("True")){
buttonAnswer = "True"; // demo
}else if (t.equals("False")){
buttonAnswer = "False";
}
return buttonAnswer;
}


}



The fxml code



Design is not done - Need it to work before styling it properly



<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="794.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="225.0" layoutY="47.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player2" />
<Text fx:id="score1" layoutX="79.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Ponits" />
<Text fx:id="score2" layoutX="503.0" layoutY="232.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Points" />
<Text fx:id="questionText" layoutX="38.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="318.0" layoutY="154.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="301.0" layoutY="505.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625" />
<Text fx:id="gameStatus" layoutX="320.0" layoutY="547.0" strokeType="OUTSIDE" strokeWidth="0.0" text="The winner is:" />
<Button fx:id="buttonTrue" layoutX="150.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" layoutX="582.0" layoutY="390.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />

</children>
</AnchorPane>


EVERYTHING IS WORKING - HERE'S MY FINAL SOLUTION



package sample;

import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class Controller {
@FXML
TextField player1;
@FXML
TextField player2;
@FXML
Text score1;
@FXML
Text score2;
@FXML
Text statusText;
@FXML
Text questionsLeft;
@FXML
Text currentPlayer;
@FXML
Button startGame;
@FXML
Button buttonTrue;
@FXML
Button buttonFalse;
@FXML
Button nextRound;

private ArrayList<String> questionsArray = new ArrayList<String>();

private ArrayList<String> answerArray = new ArrayList<String>();

private int player = 0;

private int remainingQuestions = 0;

private int playerOneScore;

private int playerTwoScore;

private String playerOneName = "";

private String playerTwoName = "";

private String answerFromArray = "";

private String answerButtonText = "";

private Random randomQuestionNumber = new Random();

private int randomQuestion = 0;


@FXML
private void handleStartGame(ActionEvent event) {

// ******* DECLARE VARIABLES ********

playerOneName = player1.getText(); //Saves the name for later use
playerTwoName = player2.getText(); //Saves the name for later use

score1.setText(playerOneName+ " has: 0 points;");
score2.setText(playerTwoName+ " has: 0 points;");

playerOneScore = 0;
playerTwoScore = 0;

System.out.println(playerOneName + " TEST"); //To TEST if the name is saved
System.out.println(playerTwoName + " TEST"); //To TEST if the name is saved

// ******* DECLARE QUESTION ARRAY ********

questionsArray.add("Question 1");
questionsArray.add("Question 2");
questionsArray.add("Question 3");
questionsArray.add("Question 4");
questionsArray.add("Question 5");
questionsArray.add("Question 6");

// ******* DECLARE ANSWER ARRAY ********

answerArray.add("True");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");
answerArray.add("False");
answerArray.add("True");

// ******* THIS IS THE PLAYER STARTING THE ROUND ******

player = 1;

// ****** RETRIEVE AND PRESENT FIRST QUESTION - THIS WILL ONLY BE USED TO START THE PROGRAM ******
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size()); // GENERATES A RANDOM NUMBER TO PICK A RANDOM QUESTION FROM ARRAYLIST
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS THE RANDOM QUESTION TO THE UI

currentPlayer.setText("Current Player: "+playerOneName);

buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
startGame.setDisable(true);
}

@FXML
private void handleButtonAnswer(ActionEvent event) {
Button answerButtonClicked = (Button) event.getSource();

answerButtonText = answerButtonClicked.getText();
answerFromArray = answerArray.get(randomQuestion); // PICKS THE ANSWER MATCHING THE QUESTION GENERATED BY THE RANDOM NUMBER

if (answerFromArray.equals(answerButtonText)) {
statusText.setText("Answer was correct! You've been awarded 1 point");
if(player == 1){
playerOneScore = playerOneScore+1;
score1.setText(playerOneName+ " has: "+playerOneScore+" points;");
}else if(player == 2){
playerTwoScore = playerTwoScore+1;
score2.setText(playerTwoName+ " has: "+playerTwoScore+" points;");
}
}else{
statusText.setText("Answer was incorrect! No point was given.");
}
nextRound.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}

@FXML
private void handleButtonNext(ActionEvent event) {
if(player == 1){
player = 2;
currentPlayer.setText("Current Player: "+playerTwoName);
}else if(player == 2){
player = 1;
currentPlayer.setText("Current Player: "+playerOneName);
}
questionsArray.remove(randomQuestion); // REMOVES QUESTION FROM ARRAYLIST
answerArray.remove(randomQuestion); // REMOVES ANSWER FROM ARRAYLIST
remainingQuestions = questionsArray.size();
questionsLeft.setText(remainingQuestions + " questions left");
if(questionsArray.size()!=0){
randomQuestion = randomQuestionNumber.nextInt(questionsArray.size());
statusText.setText("Question: " + questionsArray.get(randomQuestion)); // PRINTS QUESTION TO THE UI
}else{
if(playerOneScore>playerTwoScore){
questionsLeft.setText("Game is over. The winner is: "+playerOneName);
}else if(playerTwoScore>playerOneScore){
questionsLeft.setText("Game is over. The winner is: "+playerTwoName);
}else{
questionsLeft.setText("The game ended in a DRAW. ");
}
startGame.setDisable(false);
buttonFalse.setDisable(true);
buttonTrue.setDisable(true);
}
nextRound.setDisable(true);
buttonFalse.setDisable(false);
buttonTrue.setDisable(false);
}
}


fxml



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>


<Label layoutX="233.0" layoutY="46.0" prefHeight="66.0" prefWidth="334.0" text="Java ExpertQuiz" textAlignment="CENTER">
<font>
<Font name="Cambria Bold" size="45.0" />
</font>
</Label>
<TextField fx:id="player1" layoutX="79.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="201.0" promptText="Enter name for player 1" />
<TextField fx:id="player2" layoutX="503.0" layoutY="154.0" opacity="0.5" prefHeight="39.0" prefWidth="222.0" promptText="Enter name for player 2" />
<Text fx:id="score1" layoutX="114.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 has: 0 points" />
<Text fx:id="score2" layoutX="558.0" layoutY="224.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 has: 0 points" />
<Text fx:id="statusText" layoutX="41.0" layoutY="315.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" textAlignment="CENTER" wrappingWidth="717.1240234375">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="startGame" layoutX="363.0" layoutY="161.0" mnemonicParsing="false" onAction="#handleStartGame" text="Start game" />

<Text fx:id="questionsLeft" layoutX="304.0" layoutY="473.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Questions left: " textAlignment="CENTER" wrappingWidth="191.9072265625">
<font>
<Font size="18.0" />
</font></Text>
<Button fx:id="buttonTrue" disable="true" layoutX="150.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="True" />
<Button fx:id="buttonFalse" disable="true" layoutX="592.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonAnswer" text="False" />
<Button fx:id="nextRound" disable="true" layoutX="354.0" layoutY="378.0" mnemonicParsing="false" onAction="#handleButtonNext" text="Next Question" />
<Text fx:id="currentPlayer" layoutX="355.0" layoutY="260.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current player:">
<font>
<Font size="14.0" />
</font>
</Text>
</children>








java javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:28

























asked Nov 19 at 17:04









Kim André Langholz

133




133








  • 1




    Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
    – jewelsea
    Nov 19 at 19:01
















  • 1




    Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
    – jewelsea
    Nov 19 at 19:01










1




1




Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
– jewelsea
Nov 19 at 19:01






Run a google search on "JavaFX Event Driven Programming" and read the resources from major universities which turn up in the results. Hopefully, they will help you understand how you don't write procedural loops in JavaFX code to handle the flow of control, instead, everything is driven by events which trigger callbacks that you take action on. You can't just take a design which works for a text-based console and utilize it within a GUI framework like JavaFX, it just won't work.
– jewelsea
Nov 19 at 19:01














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Instead of thinking of it as pausing the loop, you can instead make the loop a separate function and then just call it when the button is placed. In this case, it wouldn't be a loop anymore, it would just have to store the index of the loop as a variable outside of the function, or you could call the function with the index as a parameter. I think your whole handleStartGame could be split up more. A good rule of thumb is that one function should be doing one specific task, which would make the program much easier to work with going forward. Feel free to leave a comment if you're having trouble with any of this!






share|improve this answer

















  • 1




    Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
    – Kim André Langholz
    Nov 19 at 17:18










  • Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
    – Unsolved Cypher
    Nov 19 at 18:01






  • 1




    Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
    – Kim André Langholz
    Nov 19 at 18:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379491%2fjavafx-need-a-for-loop-to-pause-and-wait-for-button-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Instead of thinking of it as pausing the loop, you can instead make the loop a separate function and then just call it when the button is placed. In this case, it wouldn't be a loop anymore, it would just have to store the index of the loop as a variable outside of the function, or you could call the function with the index as a parameter. I think your whole handleStartGame could be split up more. A good rule of thumb is that one function should be doing one specific task, which would make the program much easier to work with going forward. Feel free to leave a comment if you're having trouble with any of this!






share|improve this answer

















  • 1




    Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
    – Kim André Langholz
    Nov 19 at 17:18










  • Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
    – Unsolved Cypher
    Nov 19 at 18:01






  • 1




    Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
    – Kim André Langholz
    Nov 19 at 18:46















up vote
1
down vote



accepted










Instead of thinking of it as pausing the loop, you can instead make the loop a separate function and then just call it when the button is placed. In this case, it wouldn't be a loop anymore, it would just have to store the index of the loop as a variable outside of the function, or you could call the function with the index as a parameter. I think your whole handleStartGame could be split up more. A good rule of thumb is that one function should be doing one specific task, which would make the program much easier to work with going forward. Feel free to leave a comment if you're having trouble with any of this!






share|improve this answer

















  • 1




    Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
    – Kim André Langholz
    Nov 19 at 17:18










  • Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
    – Unsolved Cypher
    Nov 19 at 18:01






  • 1




    Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
    – Kim André Langholz
    Nov 19 at 18:46













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Instead of thinking of it as pausing the loop, you can instead make the loop a separate function and then just call it when the button is placed. In this case, it wouldn't be a loop anymore, it would just have to store the index of the loop as a variable outside of the function, or you could call the function with the index as a parameter. I think your whole handleStartGame could be split up more. A good rule of thumb is that one function should be doing one specific task, which would make the program much easier to work with going forward. Feel free to leave a comment if you're having trouble with any of this!






share|improve this answer












Instead of thinking of it as pausing the loop, you can instead make the loop a separate function and then just call it when the button is placed. In this case, it wouldn't be a loop anymore, it would just have to store the index of the loop as a variable outside of the function, or you could call the function with the index as a parameter. I think your whole handleStartGame could be split up more. A good rule of thumb is that one function should be doing one specific task, which would make the program much easier to work with going forward. Feel free to leave a comment if you're having trouble with any of this!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:12









Unsolved Cypher

450313




450313








  • 1




    Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
    – Kim André Langholz
    Nov 19 at 17:18










  • Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
    – Unsolved Cypher
    Nov 19 at 18:01






  • 1




    Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
    – Kim André Langholz
    Nov 19 at 18:46














  • 1




    Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
    – Kim André Langholz
    Nov 19 at 17:18










  • Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
    – Unsolved Cypher
    Nov 19 at 18:01






  • 1




    Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
    – Kim André Langholz
    Nov 19 at 18:46








1




1




Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
– Kim André Langholz
Nov 19 at 17:18




Thanks! Could I get you to link to more info about calling the loop as a function? Never heard about that and would love to see an example to grasp how it works. Great tips on having one function performing only one specific task. Will try to improve on that.
– Kim André Langholz
Nov 19 at 17:18












Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
– Unsolved Cypher
Nov 19 at 18:01




Sorry, I think that may have been unclear. I'm not referring to some Java structure that lets you call the loop as a function, I mean that you should take out the contents of the loop and put it into a separate function, then call that when the button is pressed. Does that make sense?
– Unsolved Cypher
Nov 19 at 18:01




1




1




Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
– Kim André Langholz
Nov 19 at 18:46




Indeed does. I'm actually making progress and got the program to work. Still working out small bugs, but it's working! Thank you so much. Will update the thread when the program is done.
– Kim André Langholz
Nov 19 at 18:46


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379491%2fjavafx-need-a-for-loop-to-pause-and-wait-for-button-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'