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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Strings not being added to arraylist

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strings not being added to arraylist

    Nevermind I figured it out
    Last edited by noel222; February 10th, 2012 at 04:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    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.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Strings not being added to arraylist

    I 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.
    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 Map

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    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

  5. #5

    Default Re: Strings not being added to arraylist

    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.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    I am using the contains method to see if the players and nicknames were added. They are not
    Have you tried printing out the contents of the arraylist to see if it contains anything?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    It simply holds nothing
    What is printed out when you use println to print it?

    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.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    Nothing is being printed out because I don't think I put the strings in the array properly

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    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.

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    Yes, I just tried it and it the exact message that came up was tR= filename@4e82710e

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Strings not being added to arraylist

    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

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    tR= filename@4e82710e
    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.

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    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:
          class 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]
    Notice the [] around the output
    Last edited by Norm; February 10th, 2012 at 03:38 PM.

  16. #16
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    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?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    If teamRoster was a ArrayList the output would look like this:
          class 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]
    Notice the [] around the output

    tR= filename@4e82710e
    That says that the teamRoster variable is a filename class object, NOT an arraylist. Do you have a class named: filename?
    Last edited by Norm; February 10th, 2012 at 03:43 PM.

  18. #18
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    No, I do not, I have a class named roster and names

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    Can you post the println statement that printed this: tR= filename@4e82710e

  20. #20
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    I wrote the exact println statement you told me to do earlier.

    System.out.println("TR=" + teamRoster);

  21. #21
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Strings not being added to arraylist

    Quote Originally Posted by noel222 View Post
    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?
    From post #7
    Yes, I used the contain method to see if the array contained the players names and nicknames and it does not.
    I see nothing in your first post that uses the contains method. If you are using it like this
    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.

  22. #22
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    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

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    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?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Strings not being added to arraylist

    As copeg suggests: post an SSCCE which will more clearly demonstrate the problem.

  25. #25
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings not being added to arraylist

    What is an SSCCE

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple inventory System. Sometimes it doesn't update my added book.
    By JustinK in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 1st, 2011, 10:50 AM
  2. Image is not added when converting eml into htm
    By Vjay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2011, 06:24 AM
  3. Image is not added when converting eml into htm
    By Vjay in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 1st, 2011, 09:23 AM
  4. How to sort an ArrayList of Strings?
    By noFear in forum Java Theory & Questions
    Replies: 6
    Last Post: September 15th, 2010, 03:23 PM