Page 4 of 4 FirstFirst ... 234
Results 61 to 70 of 70
Like Tree2Likes

Thread: Array question

  1. #61
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    OK....rethinking what I am being asked to do....I need to make a copy of the arraylist as items are found and return the copy. That is what I am struggling with I think....am I getting closer??

    Java Code:
    public ArrayList<Textbook> getTextbooks(String name)
            {
                ArrayList<Textbook>textbook3 = new ArrayList<Textbook>();
                for(Textbook myTitle : textbook)
                {    
                    if(myTitle.getTitle().equals(name))
                    {
                        textbook3.add(textbook(myTitle));  //<<<<<<<<<<<<<<<I know this part doesn't work, but not sure how to do it correctly.
                        return textbook3;
                    }
                }    
                return null;
            }

  2. #62
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    nope....don't have it....

    this is where I am stuck....don't know what else to do....

    Java Code:
    public ArrayList<Textbook> getTextbooks(String name)
    		{
    			ArrayList<Textbook>textbook3 = new ArrayList<Textbook>();
    			for(int i = 0; i < textbook.size(); i++)
    			{	Textbook myTitle = this.textbook.get(i);
    				if(myTitle.getTitle().equals(name))
    				{
    					textbook3.add(this.textbook.get(i));
    					return textbook3;
    				}
    			}	
    			return null;
    		}

  3. #63
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Array question

    It might help if you use better variable naming, because the above code just reads very confusingly. Without fixing the code let me suggest some alternative variable names.

    Java Code:
    public ArrayList<Textbook> getTextbooks(String title) {
        ArrayList<Textbook> matchingTextbooks = new ArrayList<Textbook>();
        
        for(int i = 0; i < textbook.size(); i++) {
            Textbook candidateBook = this.textbook.get(i);
            
            if(candidateBook.getTitle().equals(title)) {
                
                matchingTextbooks.add(this.textbook.get(i));
                return matchingTextbooks;
            }
        }  
        return null;
    }
    Now with that done, lets let go of the code for a moment and ask ourselves in plain English: what do we want to achieve?

    - Don't we want to add ALL book candidates which have the given 'title' to the matchingCandidates, not just one?
    -- in the above code, isn't there a variable pointing to the matching candidate that you can use?
    - When there are no matching books, don't we want an empty list to be returned in stead of null?

    The code is not exactly there yet, but almost. Can you now change it so it works?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #64
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    - Don't we want to add ALL book candidates which have the given 'title' to the matchingCandidates, not just one? yes
    -- in the above code, isn't there a variable pointing to the matching candidate that you can use? I think so, but IDK because I need to enter all the information about the matching textbooks into the new array, not just the title.
    - When there are no matching books, don't we want an empty list to be returned in stead of null? yes
    Yes that is what I need it to do, but I do not know what else to do...I have done nothing but google and search my textbook all day and I am thoroughly and completely stuck....
    Last edited by jwood; 08-09-2013 at 11:48 PM.

  5. #65
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    OK, I think this would return an empty list if no titles matched right vs the null I had before?

    Java Code:
    public ArrayList<Textbook> getTextbooks(String title)		{
    			ArrayList<Textbook>matchingTitles = new ArrayList<Textbook>();
    			for(int i = 0; i < textbook.size(); i++)
    			{	Textbook canidateTitle = this.textbook.get(i);
    				if(canidateTitle.getTitle().equalsIgnoreCase(title))
    				{
    					matchingTitles.add(this.textbook.get(i));
    					return matchingTitles;
    				}
    			}	
    			return matchingTitles;
    		}
    But I am confused. Why would this:

    matchingTitles.add(this.textbook.get(i));

    not add multiple titles (if they match)to the arraylist as it goes through the iteration?

  6. #66
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Java Code:
    public ArrayList<Textbook> getTextbooks(String title)        {
                ArrayList<Textbook>matchingTitles = new ArrayList<Textbook>();
                for(int i = 0; i < textbook.size(); i++)
                {    Textbook canidateTitle = this.textbook.get(i);
                    if(canidateTitle.getTitle().equalsIgnoreCase(title))
                    {
                        matchingTitles.add(canidateTitle);
                        
                    }
                }    
                return matchingTitles;
            }
    OK, I am guessing canidateTitles is the variable you are referring to....now it returns an empty string like the if statement is not even there....so that last return must be wrong again....
    Last edited by jwood; 08-10-2013 at 12:21 AM.

  7. #67
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    SIgh, no matter what I try it returns an empty string....what am I missing?? I can print out the entire textbook arraylist, so I know they are there....
    Java Code:
    public ArrayList<Textbook> getTextbooks(String title)
            {
                ArrayList<Textbook>matchingTitles = new ArrayList<Textbook>();
                for(int i = 0; i < textbook.size(); i++)
                {    
                    
                    if(textbook.get(i).getTitle().equalsIgnoreCase(title))
                    {
                        
                        matchingTitles.add(textbook.get(i));
                    }
                }    
                return matchingTitles;
            }
    am I missing something the the tester code maybe?
    Java Code:
    {
                                System.out.println("Enter textbook name: ");
                                String c = keyboard.next();
                                if(bookstore.getTextbooks(c).isEmpty())
                                    System.out.println("Textbook not found.");
                                else
                                    System.out.println(bookstore.getTextbooks(c));
    Last edited by jwood; 08-10-2013 at 04:10 AM.

  8. #68
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Haha...Java HATES me....can someone explain why this one works:

    Java Code:
    public ArrayList<Apparel> getApparelByCategory(String category)
            {
                ArrayList<Apparel>matchingCat = new ArrayList<Apparel>();
                for(int i = 0; i < apparel.size(); i++)
                {    
                    Apparel canidateCat = apparel.get(i);
                    if(canidateCat.getCategory().equalsIgnoreCase(category))
                    {
                        matchingCat.add(canidateCat);
                    }
                }    
                return matchingCat;
    and this one returns an empty list everytime???

    Java Code:
    public ArrayList<Apparel> getApparels(String appName)
            {
                ArrayList<Apparel>matchingApparel = new ArrayList<Apparel>();
                for(int i = 0; i < apparel.size(); i++)
                {    
                    Apparel canidateName = apparel.get(i);
                    if(canidateName.getName().equalsIgnoreCase(appName))
                    {
                        matchingApparel.add(canidateName);
                        
                    }
                }    
                return matchingApparel;
            }
    the only thing really different is the search criteria....I just do not get it!!

  9. #69
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array question

    Print out the entire apparel list before you start searching it:

    Java Code:
    System.out.println(apparel);
    ... and make sure that the single Apparel objects have the toString() method implemented (otherwise the apparel list won't print properly and we don't want another monster thread like this).
    I suspect that that apparel list is not what you assume it is.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #70
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by JosAH View Post
    Print out the entire apparel list before you start searching it:

    Java Code:
    System.out.println(apparel);
    ... and make sure that the single Apparel objects have the toString() method implemented (otherwise the apparel list won't print properly and we don't want another monster thread like this).
    I suspect that that apparel list is not what you assume it is.

    kind regards,

    Jos
    I had already done that, which is how I knew the array list was functional.

    The problem ended up being my tester. Something as simple as using keyboard.next(); instead of keyboard.nextLine() for my user input.

    Class is over, so you no longer have to worry about any "monster" threads from me.

Page 4 of 4 FirstFirst ... 234

Similar Threads

  1. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-07-2011, 12:56 AM
  2. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 02:42 AM
  3. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 04:55 PM
  4. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 02:41 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 03:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •