0

this is my first post on this web site, so please be patience :) I'm studying java and I'm trying to create a small program that storage in two arrays the name of player and the attendance of them. I'm using JOptionPane for the 'user interface'. I would like, when the user ask for, to show the names and the respective attendance of them. Here it's my code(it's not completed):

import javax.swing.*;
import java.text.*;

public class Pelada{
    public static void main(String []args){
        String[] players = new String[10];
    int[] attendance = new int[10];
    int x = 0, z = 0, control = 0, posPlayer = 0;
    String test;

    while(control != 4){
        control = Integer.parseInt(JOptionPane.showInputDialog(null,"1- Add       new players \n 2- List \n 3- Increment attendance \n 4- Delete player \n 4- Exit", "Choose an option below", JOptionPane.INFORMATION_MESSAGE));
            if(control == 1){
                players[x] = JOptionPane.showInputDialog(null, "New player: ", "Add New Player", JOptionPane.INFORMATION_MESSAGE);
                attendance[x] = Integer.parseInt(JOptionPane.showInputDialog(null, "How many matchs have he played so far? ", "Attendance", JOptionPane.INFORMATION_MESSAGE));
                x++;
            }   
            else if(control == 2)
                    for (int i=0; i < players.length; i++){
                    JOptionPane.showMessageDialog(null, "Attendance = " + attendance[i], "N: " + i + "- " + players[i], JOptionPane.WARNING_MESSAGE);       
                    }
                else if(control == 3){
                        posPlayer = Integer.parseInt(JOptionPane.showInputDialog(null, "Choose the player id: ", "Player Id", JOptionPane.INFORMATION_MESSAGE));
                        attendance[posPlayer] = Integer.parseInt(JOptionPane.showInputDialog(null, "Increment ", "Attendance", JOptionPane.INFORMATION_MESSAGE));
                    }                   
    }

}

}

3
  • What exactly is your question? Your code already lists all players together with their attendance, doesn't it? Commented Oct 3, 2011 at 7:59
  • If you are using a recent enough version of Java, have a look at the for-each loop construct: for (type x : listOfXs) { ... } instead of manually iterating over the array and getting each element. Going with @dogbane's suggestion to make it more OO-correct will make this trivial, and greatly reduce the clutter caused by boilerplate code. Commented Oct 3, 2011 at 8:49
  • Also, just a suggestion, but do fix your indentation (and perhaps whitespacing as well). It will make the code MUCH easier to read if you indent consistently (regardless of how exactly you indent) instead of, as it is, haphazardly. Commented Oct 3, 2011 at 8:51

1 Answer 1

1

Instead of having two arrays; one for players and one for attendance, make your code more object-oriented by creating a Player class encapsulating the name and attendance of a player:

public class Player {
    private final String name;
    private final int attendance;

    public Player(String name, int attendance) {
        this.name = name;
        this.attendance = attendance;
    }

    public String getName() {
        return name;
    }

    public int getAttendance() {
        return attendance;
    }
}

Then create Player objects and store them in an ArrayList. Don't use an array unless you know how many players are going to be added.

List<Player> players = new ArrayList<Player>();

if (control == 1) {
     String name = JOptionPane.showInputDialog(null, "New player: ", "Add New Player",
             JOptionPane.INFORMATION_MESSAGE);
     int attendance = Integer.parseInt(JOptionPane.showInputDialog(null,
             "How many matchs have he played so far? ", "Attendance", JOptionPane.INFORMATION_MESSAGE));
     Player player = new Player(name, attendance);
     players.add(player);

} else if (control == 2) {
    for (int i = 0; i < players.size(); i++) {
        Player player = players.get(i);
        JOptionPane.showMessageDialog(null, "Attendance = " + player.getAttendance(), "N: " + i + "- " + player.getName(),
                JOptionPane.WARNING_MESSAGE);
    }                
}
Sign up to request clarification or add additional context in comments.

3 Comments

This was a great solution. It took me a while to understand though as I'm a beginner. One doubt regarding to the code where is written Player player = player.get(i); Are u creating another object from the Player class. If I'm right, why don't u need to use new? Thanks a lo
Player player = players.get(i); returns the player at index i from the players arraylist. It doesn't create a new object, but retrieves an existing one from the list.
so why do we need to write Player(which is a class) and not only use player = player.get(i); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.