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 13 of 13

Thread: ArrayList problem, getList and returning. help wanted

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

    Default ArrayList problem, getList and returning. help wanted

    Hello

    I got this problem
    The last method to be added is a method that takes an employee name as argument and returns a list containing all journals that the employee is to see. You may use the following method header:

    public ArrayList<Journal>getJournalList(String empName)

    Normally I would just use the following:

     public List<ArrayList<String>> getJournalList()
        {
            return journalList;
        }

    But when i need to return a list containing all journals that employee is to see, how do you do that? Any help or tips would be much appreciated.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    Not enough info provided. What's the criteria that determines what journals an employee "is to see?" What does that mean?

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    1) create a List<Journal> list1
    2) loop over all journals
    3) if(journal is to see for empName) -> add journal to list1
    4) end loop
    5) return list1

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

    Default Re: ArrayList problem, getList and returning. help wanted

    Sorry,

    I this class called journal

    public class Journal { private String titel; private String publisher - Pastebin.com

    And what im trying to make is a journal Collection

    public class JournalCollection { // instance variables - replace the examp - Pastebin.com

    And hence the problem with my question

  5. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    Ok, then it is:
    1) create a List<Journal> list1
    2) loop over all journals
    3) if(journal.circulationList contains empName) -> add journal to list1
    4) end loop
    5) return list1

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

    Default Re: ArrayList problem, getList and returning. help wanted

    Thank you, i've gotten this so far, but it won't seem to work

    public JournalCollention getJournalList(string emp)
    {
    JournalCollection result = new Journalcollection();
    for (Journal journal : journals)
    {
    if (journal.match(emq)) resutl.addJournal(journal);
    }
    return result;
    }
     
    journal.match(..)

  7. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    What doesn't seem to work? What do you get? What do you expect?

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

    Default Re: ArrayList problem, getList and returning. help wanted

    public ArrayList<Journal>getJournalList(String empName)
        {
        JournalCollection result = new Journalcollection();
        for (Journal journal : journals)
        }
        {
        if (journal.match(empName)boolean match(String empName);
        {
        return circulationList.contains(empName);
        } 
        {
        result.addJournal(journal);
        }
        {
        return result;
        }

    Basicly, I understand what you mean by all your points, but I cant seem to write it so it works. And I cant decypher the syntax well enough, since im really struggling with this arrayList.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    The formatting of the posted code needs work. The {}s don't look right.
    Statements inside of {}s should be indented to make the code easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList problem, getList and returning. help wanted

    Yeah, im aware the code looks horrible in its current state. But I only wanted to list what i had sofar, im really struggling how to set up arrayLists since im a newbie and this is my first time messing around with them.

    What im looking for is some really good pointers as to how this is written so it works.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    Making the code readable will help others read and understand what the code is trying to do.
    Messy code makes it hard to read and understand. I'd assume that it would take < 5 minutes to edit the code and make it look right.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList problem, getList and returning. help wanted

    I get that, but the problem is, I have been sitting with this problem for the last hours trying to rewrite it 2000 times. And so I can't really clean up the code since im at a total loss atm, as im having real issues with how an arraylist when getList is written. I would clean it up if I knew how.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList problem, getList and returning. help wanted

    I'd recommend that you toss that code and start over. Its {}s are completely messed up.

    When entering code make sure that the { that starts a method definition has a } to go with it.
    When coding an if statement, make sure that the { that starts enclosing the code in the if has a } to go with it.

    Statements withing {}s should be indented, not in the same column as the { enclosing them.

    These are very basic things that will make the code readable. Unformatted code is hard to read.


    Here's your code with some formatting and comments:
    public ArrayList<Journal>getJournalList(String empName)
    {
        JournalCollection result = new Journalcollection();
        for (Journal journal : journals)
    }   // end getJournalList()
     
    {	   //????????? What does this enclose?	    It's } is missing
     
        if (journal.match(empName)boolean match(String empName);
        {
        	return circulationList.contains(empName);
        }
     
        // Following two statements don't below with any other code????
        {
        	result.addJournal(journal);
        }
     
        {
        	return result;
        }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with returning 2d array
    By UltimoBrah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2013, 12:00 PM
  2. returning a rectangle through an arraylist
    By burntcasadilla in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 14th, 2012, 10:53 PM
  3. [SOLVED] ArrayList.get() Returning "Incompatible Type"
    By Destro in forum Collections and Generics
    Replies: 2
    Last Post: June 15th, 2012, 06:13 PM
  4. ArrayList<Class> is returning null
    By havinFun in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 14th, 2012, 04:59 PM
  5. [SOLVED] Returning ArrayList via user input
    By IanSawyer in forum Collections and Generics
    Replies: 4
    Last Post: March 27th, 2012, 05:40 PM