Search code examples
javaarraysswingjoptionpane

Array not recording input from JOptionPane correctly


So what im trying to do is create a button that allows the player of my game to enter the number of players they want competing in their tournament. Then they enter the names of the contenders which gets recorded and stored in an array for use later. I created a playerList method to check if it was recording the names correctly, but it keeps returning only one name. This leads me to believe that my for loop in my enterPlayers() method is only adding the first name entered into the array. Im not sure why this would be, however. Im intending to use these names later to set up matches between players for advancement in the tournament, so this is a vital part of my project. If anyone could help it would be much appreciated. Thanks and have a great day!

Here is the code involved in this:

    public void playerList()
  {
      JFrame frame = new JFrame("The Competitors");
      JPanel panel = new JPanel();
      JButton returnToMenu = new JButton ("Return to menu");
      String competitors = "<html><h1><font size=25 color=red> Your competitors are:<br>";
      for (int i = 0; i<tournament.length; i++)
      {
          competitors+= (i+1) + ". " + tournament[i] + "<br></font></h1></html>";  
      }
      JLabel overText = new JLabel(competitors);
      returnToMenu.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent event)
          {
                 sound11.play();
                 frame.dispose();
          }
      });
      panel.add(returnToMenu);
      panel.add(overText);
      frame.getContentPane().setBackground(Color.black);
      frame.add(overText, BorderLayout.NORTH);
      frame.add(panel);
      frame.setSize(800, 340);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      sound13.loop();
  }

  public void enterPlayers()
  {
      String number = JOptionPane.showInputDialog("Enter number of players ");   
      int a = Integer.parseInt(number);
      numPlayers = a;
      JOptionPane.showMessageDialog(null, "Number of players = " + numPlayers);
      while (numPlayers>10 || numPlayers<2 || ((numPlayers>2 && numPlayers<=10) && numPlayers%2!=0))
      {
          if (numPlayers>10)
          {
              JOptionPane.showMessageDialog(null, "Enter no more then 10 players"); 
          }
          else if (numPlayers<2)
          {
              JOptionPane.showMessageDialog(null, "You must have more then 2 players"); 
          }
          else if ((numPlayers>2 && numPlayers<=10) && numPlayers%2!=0)
          {
              JOptionPane.showMessageDialog(null, "You must have an even number of players"); 
          }
          String number1 = JOptionPane.showInputDialog("Enter number of players ");  
          int b = Integer.parseInt(number1);
          numPlayers = b;
          JOptionPane.showMessageDialog(null, "Number of players = " + numPlayers);        
      }

      tournament = new String[numPlayers];

      for (int i = 0; i<numPlayers; i++)
      {
          String name = JOptionPane.showInputDialog("Enter Player " + (i+1) + " name: ");
          String player = "Player " + (i+1) + " is: " + name;
          JOptionPane.showMessageDialog(null, player);
          tournament[i] = name;
      }
  }

Solution

  • The problem is you are closing the html on every single player in this line:

    for (int i = 0; i<tournament.length; i++)
    {
        competitors += (i+1) + ". " + tournament[i] + "<br></font></h1></html>";  
    }
    

    You should do this instead:

    for (int i = 0; i<tournament.length; i++)
    {
        competitors += (i+1) + ". " + tournament[i] + "<br>";  
    }
    
    competitors += "</font></h1></html>";