Nevermind I figured it out
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Nevermind I figured it out
Last edited by noel222; February 10th, 2012 at 05:25 PM.
How are you detecting what the arraylist contains or does not contains?
Your bits and pieces of code do not show enough to make suggestions about what the problem is.
Nowhere in your code do I see a String being added to an ArrayList - I do see instances of Player. If you want to retrieve a Player based upon name, loop over the List and check for the name, or use a MapI am having difficulty adding strings to an array list. I am not getting any error, however, when I check to see if the array list contains the strings that should be in it, it does not.
I am using the contains method to see if the players and nicknames were added. They are not and I am not sure how to add them
You left out some key details. What does your constructor for Roster actually do? I see that you define teamRoster in class Roster immediately. Do this in the constructor to ensure that it gets executed.
The following code is untested but holds the basic concept of what you are looking for.
public class Team { public Roster roster; Team() { this.roster = new Roster(); } public static void main(String[] args) { Team myTeam = new Team(); System.out.println(myTeam.roster.add(new Player("Java","Java prog"))); System.out.println(myTeam.roster.add(new Player("1","1.2"))); System.out.println(myTeam.roster.add(new Player("2","2.2"))); } } class Roster { ArrayList<Player> playerList; Roster() { this.playList = new ArrayList<Player>(); } public boolean add(Player player) { return this.playerList.add(player); } } class Player { private String name; private String nickName; Player(String name, String nickName) { this.name = name; this.nickName = nickName; } Player() { this("",""); } }
The following should net you output of 3 "true"s, each on a separate line.
Have you tried printing out the contents of the arraylist to see if it contains anything?I am using the contains method to see if the players and nicknames were added. They are not
Yes, I used the contain method to see if the array contained the players names and nicknames and it does not. It simply holds nothing. I put in 2 get methods to get the player names and nicknames but I am still not sure how to add these to the arraylist
What is printed out when you use println to print it?It simply holds nothing
Add a println() call immediately after the code that adds to the arraylist:
... teamRoster.add(new Player("Keith", "The Man")); System.out.println("tR=" + teamRoster); // show the contents.
Nothing is being printed out because I don't think I put the strings in the array properly
Did you try what I just posted to see what was in the arraylists?
Add a println() call immediately after the code that adds to the arraylist:
... teamRoster.add(new Player("Keith", "The Man")); System.out.println("tR=" + teamRoster); // show the contents.
Yes, I just tried it and it the exact message that came up was tR= filename@4e82710e
Read my post above...the ArrayList does not contain Strings - it contains players. If you are using contains() to check for the presence of a String, you will always get false. But I can only guess this is what you are trying to do, because you have not shown us the code which attempts to find the 'String' in the ArrayList
That says that the teamRoster variable is a filename class object, NOT an arraylist. Do you have a class named: filename?tR= filename@4e82710e
You need to post the definition for the teamRoster variable whose add() method is being called.
Even when I simply say to print out the contents of the array, I get values such as filename@343kgdf instead of the actual values. I am not sure how to get the values
That says that the teamRoster variable is a filename class object, NOT an arraylist. Do you have a class named: filename?
You need to post the definition for the teamRoster variable whose add() method is being called.
If teamRoster was a ArrayList the output would look like this:
Notice the [] around the outputclass Testing {} ArrayList<Testing> al = new ArrayList<Testing>(); al.add(new Testing()); al.add(new Testing()); System.out.println("al=" + al); // al=[TestCode10$1Testing@addbf1, TestCode10$1Testing@42e816]
Last edited by Norm; February 10th, 2012 at 04:38 PM.
I posted my code in my first post. I am not really sure what you guys are asking me to post. That is all I have. I am not sure why the player names and nicknames are not in the array?
If teamRoster was a ArrayList the output would look like this:
Notice the [] around the outputclass Testing {} ArrayList<Testing> al = new ArrayList<Testing>(); al.add(new Testing()); al.add(new Testing()); System.out.println("al=" + al); // al=[TestCode10$1Testing@addbf1, TestCode10$1Testing@42e816]
That says that the teamRoster variable is a filename class object, NOT an arraylist. Do you have a class named: filename?tR= filename@4e82710e
Last edited by Norm; February 10th, 2012 at 04:43 PM.
No, I do not, I have a class named roster and names
Can you post the println statement that printed this: tR= filename@4e82710e
I wrote the exact println statement you told me to do earlier.
System.out.println("TR=" + teamRoster);
From post #7
I see nothing in your first post that uses the contains method. If you are using it like thisYes, I used the contain method to see if the array contained the players names and nicknames and it does not.
if ( teamRoster.contains("Mark") ){ }
contains will always contain false, as it contains Player objects, not String objects. It helps to post an SSCCE which will more clearly demonstrate the problem.
sorry about that. I simply used the contain method to check and then deleted it once I saw the array did not contain the names and nicknames
There is no space in the code you posted.
There is a space in this: tR= filename@4e82710e
Explain that please!
Does the teamRoster class object have a toString() method that returns that String you show in the post?
As copeg suggests: post an SSCCE which will more clearly demonstrate the problem.
What is an SSCCE