Reputation: 17
I am having a very strange problem, I have a method noOfPlayers that asks for the number of players in the game, Once I have the number of players in the game I ask for each of their names in turn. Once I have been given the name of the player a frame is created asking them to specify what counter they'd like to pick. When they click the purple gem (for testing purposes) it should print out the players name in the console but the for loop doesnt seem to work. Any idea how i'd get the loop to work correctly?
public class setupPlayers extends JFrame implements ActionListener {
int intOfPlayers, purpleClick = 0, orangeClick = 0, iceClick = 0, greenClick = 0;
ArrayList<Player> arrayOfPlayers = new ArrayList<Player>();
JButton purpleGemBTN, greenGemBTN, iceCubeBTN, orangeGemBTN;
JFrame organisationPanel;
JPanel titleChoiceCounter, counterSelector;
ImageIcon finalCounter;
private static Dialog d;
public setupPlayers() {}
public void noOfPlayers() {
try {
String inputValue = JOptionPane.showInputDialog("Please input the number of players");
intOfPlayers = Integer.parseInt(inputValue);
if (intOfPlayers > 4) {
JOptionPane.showMessageDialog(null, "Only 1-4 can play!", "Error!", JOptionPane.ERROR_MESSAGE);
noOfPlayers();
intOfPlayers = 0;
}
for (int z = 0; z < intOfPlayers; z++) {
String playerName = JOptionPane.showInputDialog("Player " + (z + 1) + " please input your name");
chooseCounter();
arrayOfPlayers.add(new Player(playerName, (z + 1), null, 0));
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "You did not enter the number of players, please enter the number of players", "Error!", JOptionPane.ERROR_MESSAGE);
noOfPlayers();
}
}
public void chooseCounter() {
Frame window = new Frame();
ImageIcon purpleGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\pink.png");
ImageIcon greenGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\yellow.png");
ImageIcon orangeGemImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\brown.png");
ImageIcon iceCubeImg = new ImageIcon("C:\\Users\\Anonymous\\Documents\\white.png");
d = new Dialog(window, "Please select your counter", true);
d.setLayout(new GridLayout(2, 2));
d.setLocation(400, 300);
d.setSize(500, 500);
purpleGemBTN = new JButton("purple", purpleGemImg);
greenGemBTN = new JButton(greenGemImg);
orangeGemBTN = new JButton(orangeGemImg);
iceCubeBTN = new JButton(iceCubeImg);
purpleGemBTN.addActionListener(this);
greenGemBTN.addActionListener(this);
iceCubeBTN.addActionListener(this);
orangeGemBTN.addActionListener(this);
d.add(purpleGemBTN);
d.add(greenGemBTN);
d.add(orangeGemBTN);
d.add(iceCubeBTN);
d.setVisible(true);
}
public static void main(String[] args) {
setupPlayers spObj = new setupPlayers();
}
public void actionPerformed(ActionEvent e) {
JButton pressed = new JButton();
pressed = (JButton) e.getSource();
if (pressed.getText().equals("purple")) {
for (int z = 0; z < arrayOfPlayers.size() - 1; z = z) {
String currentPlayer = arrayOfPlayers.get(z).playerNme;
System.out.println(currentPlayer);
}
d.setVisible(false);
}
}
}
Upvotes: 0
Views: 118
Reputation: 191733
So, from your comments on the previous answers, I'm guessing your problem is more related to how you are recursively obtaining the names.
Here is a quick pair of methods that seem to achieve what you are wanting without recursion.
private ArrayList<Player> arrayOfPlayers = new ArrayList<>();
private int intOfPlayers;
public void noOfPlayers() {
while (true) {
String inputValue = JOptionPane.showInputDialog("Please input the number of players");
if (inputValue != null) { // Text was entered, cancel not clicked
try {
intOfPlayers = Integer.parseInt(inputValue);
if (intOfPlayers > 4 || intOfPlayers < 1) {
JOptionPane.showMessageDialog(null, "Only 1-4 can play!", "Error!", JOptionPane.ERROR_MESSAGE);
} else {
break; // stop asking for numbers
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a number!", "Error!", JOptionPane.ERROR_MESSAGE);
e.printStackTrace(); // never ignore errors, even if obvious
}
} else {
System.out.println("Quitting from number players input");
System.exit(0); // Canceled the dialog, so quit the program
}
}
for (int z = 0; z < intOfPlayers; z++) {
String playerName = JOptionPane.showInputDialog("Player " + (z + 1) + " please input your name");
if (playerName != null) {
// chooseCounter(playerName);
arrayOfPlayers.add(new Player(playerName, (z + 1), null, 0));
} else {
System.out.println("Quitting from player " + (z + 1) + " name input");
System.exit(0); // Canceled the dialog, so quit the program
}
}
printPlayerNames();
}
private void printPlayerNames() {
for (int z = 0; z < arrayOfPlayers.size(); z++) {
String currentPlayer = arrayOfPlayers.get(z).playerNme;
System.out.println(currentPlayer);
}
}
Upvotes: 0
Reputation: 8387
Try to write this for loop:
for (int z=0; z<arrayOfPlayers.size()-1;z=z){...
Like this:
for (int z=0; z<arrayOfPlayers.size();z++){...
Increase the z++
and remove -1
from arrayOfPlayers.size()-1
because the index z
start from 0
:
Upvotes: 2
Reputation: 5021
As you have for (int z=0; z<arrayOfPlayers.size()-1;z=z)
you will iterate infinitely because z
will always be equal to 0
.
Try to change the z=z
to z++
so that the iteration ends once z = arrayOfPlayers.size()-1
.
your code will be:
for (int z=0; z<arrayOfPlayers.size();z++){
String currentPlayer = arrayOfPlayers.get(z).playerNme;
System.out.println(currentPlayer);
}
Upvotes: 0
Reputation: 516
or better with for each loop
for(Player p : arrayOfPlayers)
{
String name = p.playerNme;
System.out.println(name);
}
Upvotes: 2
Reputation: 131
Your not iterating through the for loop z++
not z=z
:
for (int z=0; z<arrayOfPlayers.size()-1;z++){
String currentPlayer = arrayOfPlayers.get(z).playerNme;
System.out.println(currentPlayer);
Upvotes: 0