Displaying individual elements of an object in an Arraylist through a for loop?
Posted
by
user1180888
on Stack Overflow
See other posts from Stack Overflow
or by user1180888
Published on 2012-10-15T21:32:00Z
Indexed on
2012/10/15
21:36 UTC
Read the original article
Hit count: 141
I'm trying to Display individual elements of an Object I have created.
It is a simple Java program that allows users to add and keep track of Player Details.
I'm just stumped when it comes to displaying the details after they have been added already. here is what my code looks like
I can create the object and input it into the arraylist no problem using the case 2, but when I try to print it out I want to do something like
System.out.println("Player Name" + myPlayersArrayList.PlayerName + "Player Position" + myPlayerArrayList.PlayerPosition + "Player Age" + "Player Age");
I know that is not correct, but I dont really know what to do, if anyone can be of any help it would be greatly appreciated. Thanks
System.out.println("Welcome to the Football Player database");
System.out.print(System.getProperty("line.separator"));
UserInput myFirstUserInput = new UserInput();
int selection;
ArrayList<Player> myPlayersArrayList = new ArrayList<Player>();
while (true) {
System.out.println("1. View The Players");
System.out.println("2. Add A Player");
System.out.println("3. Edit A Player");
System.out.println("4. Delete A Player");
System.out.println("5. Exit ") ;
System.out.print(System.getProperty("line.separator"));
selection = myFirstUserInput.getInt("Please select an option");
System.out.print(System.getProperty("line.separator"));
switch(selection){
case 1:
if (myPlayersArrayList.isEmpty())
{
System.out.println("No Players Have Been Entered Yet");
System.out.print(System.getProperty("line.separator"));
break;}
else
{for(int i = 0; i < myPlayersArrayList.size(); i++){
System.out.println(myPlayersArrayList);
}
break;
case 2: {
String playerName,playerPos;
int playerAge;
playerName = (myFirstUserInput.getString("Enter Player name"));
playerPos = (myFirstUserInput.getString("Enter Player Position"));
playerAge = (myFirstUserInput.getInt("Enter Player Age"));
myPlayersArrayList.add(new Player(playerName, playerPos, playerAge)); ;
break;
}
© Stack Overflow or respective owner