• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use a For Loop to find Books  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a project with several classes. This class is BookStore and one of the instance variables is an array: "private Book[] inventory". There is a method I'm stuck on: "public int howManyBooksDidThisAuthorWrite(String authorLastName)".  I have to use a for loop and .equalsIgnoreCase. I can't find a way to complete it. Can someone give advise please?
 
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are dealing with an object called book, and it's an array of books, it's like looping through an index of integers. Let's assume that the array is called books you have a book called "Call of the Wild" You could loop through the index of books with a for loop (either enhanced or regular).



The above is an enhanced for loop. I don't know exactly what you are looking for though.
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naziru! Thanks for the reply. With this method, I have to use a for loop, the regular one. Part of this project is solving these methods by the particular loop we have learned: while, for, foreach. This particular one requires the normal for loop. And seems like we have to use the String authorLastName and also the method: getLastName() because that's in the Name class in my project.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not sure why you are concentrating on the need for a for-loop when you've already got (and posted) a perfectly good for-loop. What you do need instead is a way to find out whether the Book object you have there matches something. And since your method has a String parameter called "authorLastName" it suggests to me that you need to find the last name of the Book's author and then compare it to that String object.

Note that my description of your requirement is different from your description ("I have to use a for loop and .equalsIgnoreCase"). But maybe my guess at the problem you have to solve isn't the right guess. I find it hard to believe that your description is the whole story: normally requirements are stated in terms of "real-world" things and not in terms of the programming tools you're supposed to use to fulfil those requirements.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome Changxi! Thanks for that and I'm close. But when I compile, BlueJ says there is no .getLastName() method because that's in the Name class. But my constructor has a Name object. I'll post the whole class to this point below. How can I put in the getLastName method from another class this way?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Peters wrote:Awesome Changxi! Thanks for that and I'm close. But when I compile, BlueJ says there is no .getLastName() method because that's in the Name class. But my constructor has a Name object. I'll post the whole class to this point below. How can I put in the getLastName method from another class this way?


The only way to use the method from another class is to INHERIT it from that class. Thus, you have to use something infamous to object oriented programming known as inheritance. Look up inheritance and polymorphism in Java.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Peters wrote:Awesome Changxi! Thanks for that and I'm close. But when I compile, BlueJ says there is no .getLastName() method because that's in the Name class. But my constructor has a Name object. I'll post the whole class to this point below. How can I put in the getLastName method from another class this way?


You're thinking about machinery instead of meaning again. The question isn't "How can I use the getLastName method from another class?" The question is actually "How can I get the last name of the author of a Book?"

You have a Book object. So you have to find its author, and then find the last name of that author. Two steps.

(Inheritance and all that aren't necessary here, just understanding the design of a Book object.)
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. So about just looking at the Book object and then finding the author. This is where I'm at(line 4) and when I try and compile, it says: "incompatible types: java.lang.String cannot be converted to Author". Now I have the Name class but also the Author class so now I'm really stuck with this.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't understand the reason you decided to create a Book object at line 4. You already have a perfectly good Book object -- why create a new one which contains irrelevant data?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!