File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you add objects to arrays?

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

}

}

}[/code]
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if you find some help in this Array tutorial.....


Edit: And WELCOME to JavaRanch!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

To access elements of an array, use the array name followed by the index enclosed in brackets. For example...

array[0] = "Shannon";
array[1] = "Tina";
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Coddaire wrote:

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.
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access elements of an array, use the array name followed by the index enclosed in brackets. For example...

array[0] = "Shannon";
array[1] = "Tina";


The problem with this approach is I do not have definite names. So instead of that, I could put in the string variable, name.

So if i did

array[0] = name;
array[1] = name;
array[2] = name;
array[3] = name;

It would put one name for four "slots." Not four names.
How can I create a loop that assigns one name to one "slot" in the array?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hint: use the loop index

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So like this?

 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect
 
Campbell Ritchie
Sheriff
Pie
Posts: 47250
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines in code tags; they make the code difficult to read.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic