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.

Results 1 to 10 of 10

Thread: how do i add ID numbers to ArrayList entires

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy how do i add ID numbers to ArrayList entires

    for my Uni assignment we have to create a library system with 3 other classes membersRecords, bookRecords and LoanedBooks i'm working on members records and have sorted the bassic array lsit formate and to enter new members and to print all members now i want to be able to add in a way to enter id numbers into an existing members entry how do i do that? i am working on the blue jay programe to make this java code
    code so far :

    java.util.*;

    public class membersRecord
    {
    Private ArrayList <String> details;

    public membersRecord ()
    {
    details =new ArrayList <String> ();
    }

    public void newMember (String firstname, String surname, String phoneNumber)
    {
    details.add(firstname);
    details.add(surname);
    details.add (phoneNumber);
    }

    public void printAllMembers ()
    {
    System.out.println (details);
    }
    }

    please help


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,860
    My Mood
    Mellow
    Thanks
    179
    Thanked 631 Times in 618 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    Please post your code in code tags and respect Java's naming conventions. Your code is hard to read and understand as it is currently posted.

    Cramming the member details into an ArrayList continues the bad practice of storing data in parallel arrays or ArrayLists. Instead, store each member detail in its own field. If you then need to collect the member records into an ArrayList, do ArrayList<MemberRecord>.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    how do i do that though i'm really knew at programing i dont even know what the java conventions are sorry

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    308
    My Mood
    Bored
    Thanks
    45
    Thanked 86 Times in 72 Posts
    Blog Entries
    4

    Default Re: how do i add ID numbers to ArrayList entires

    Think of an ArrayList as a growing, re-sizable list of things. They are really cool but you need to consider what the 'things' that you are storing are and how they are accessed.

    You are currently storing strings. So after you call newMember once it will contain:

    firstname, surname, phoneNumber

    When you call it a second time it will contain:

    firstname, surname, phoneNumber, firstname, surname, phoneNumber

    Call it a dozen times and it will contain:

    firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber, firstname, surname, phoneNumber

    Doesn't make much sense huh? How do you get the phone number of a particular individual from this ArrayList? What happens when you remove at item from this list?

    Instead of storing strings do as Greg suggested and store an Object that contains all of this information. Adding something new to the object like an ID becomes as simple as changing the Object.

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Amethyst (November 18th, 2013)

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    but I don't know how to store all the details in one array but for part of the assignment we do have to search for individual names and id numbers to find individual members that's why I did it this way to make it so that search on surnames can be carried out and bring up the full set of details or that's the theory anyway I under stand the logic of what you have both said but I don't know how to do it and stick to the assignment

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    You would create a Member class that has attributes: firstname, surname, phoneNumber, ID. Make your ArrayList type Member and not String. Create Member objects and store them into the list instead of Strings.
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    Amethyst (November 18th, 2013)

  9. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    i cant use the array list as members instead of String blue jay wont allow it it says it can not find the class symbol that is why i did the list they way it has been done

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,860
    My Mood
    Mellow
    Thanks
    179
    Thanked 631 Times in 618 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    Blue Jay is not the boss of you or Java, and you should not allow tools or your lack of ability to use the tool correctly to dictate your programming. "Cannot find symbol X" errors mean that X is unknown. To make it known, declare it, import it, include it in the same package, whatever it takes.

    If you want help with an error message, post the code and the error, copied from just as it appears at your end. Please use code tags.

  11. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    i dont know how to use code tags i'm a novice at java i'm really sorry i must be annoying you guys but i have used import java.* which is supost to put everything in to blue jay, i have managed now to add id numbers but it either puts the same id number on 6 lines instead of printing the entry of one member on 1 line its printing on 3 and so two members is 6 lines in the print out with the same id number or else it prints a differnt id number on each line instead of a differnt id number for each member if i can't get it sorted i'm going to fail programing i'm sorry i'm annying but blue jay is totally stressing me out thank you for helping me

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,860
    My Mood
    Mellow
    Thanks
    179
    Thanked 631 Times in 618 Posts

    Default Re: how do i add ID numbers to ArrayList entires

    Read this for info on how to use code tags and other useful newcomer info.

    The import statement:

    import java.*;

    does not import "everything." To know exactly what the import needs to be to import a specific class, go to that class' API page. In this case, visit the ArrayList API page. Notice the line just above the big, bolded class name on that page is the hierarchy "java.util" which is then shown from java.lang.Object all the way to java.util.ArrayList just below the big, bolded class name. So, the import statement needed to use ArrayList is:

    import java.util.ArrayList;

    As for the other problems with your code, after you've applied the import and modified the code to use ArrayList properly (or as best you can), then come back WITH CODE and problem descriptions that you need help with. Please post the code properly which you'll know how to do after reading the first link in this post. Show sample runs, if possible.

    Quit angling for the sympathy vote with your novice status and get to work. We've all been there and know what it takes to climb the ladder.

  13. The Following User Says Thank You to GregBrannon For This Useful Post:

    Amethyst (November 23rd, 2013)

Similar Threads

  1. How to add the numbers in the following series?
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2013, 07:23 PM
  2. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  3. How to add line numbers in Eclipse
    By Brt93yoda in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 10:30 PM
  4. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM
  5. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM