This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I'm creating a Connect Four program, and the first part of it prompts the user for the number of players. The minimum allowed is two and the max is four. Once that is determined, it then progresses to allow the users to input their names. These names need to be stored in an array (or arrayList, it doesn't matter). Here's my code so far. Keep in mind that some string isn't being used yet. ANy suggestions?
String name, start, size, win;
String playerSTR;
int player;
String[] array = {null, null, null, null};
playerSTR = JOptionPane.showInputDialog("How many players?");
player = Integer.parseInt(playerSTR);
for (int i=0; i<player; i++)
{
name = JOptionPane.showInputDialog("Enter a name:");
// this is where the array should add name
Note that the shortcut syntax for arrays can be helpful if you know the elements at the time of declaration. For example...
But if you're simply creating the array for elements to be added later, you can use the "new" syntax and just specify the array size. The elements will be initialized to null by default.
Also, "array" might not be the most descriptive name for your variable. You might consider calling it something like "playerNames" instead.