• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching my ArrayList of objects.  RSS feed

 
Adam Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Okay, I will be as succinct as possible. I am writing a rudimentary book store program for homework. The program consists of two class files and a main. The main issue I am having is on getting the search portion of my program to work. I will post as little as I can and still make sense. This is the block of code belongs to my public Book getBook() method, which is of the BookStore class.



Line 2 in the previous block gave the error "non-static method getTitle(Book) cannot be referenced from a static context". I tried to remedy that error with initializing a string like so:



Unfortunately, that gave me the same error as before.

This next block of code belongs to my public String getTitle() method of the Book class. I think some of the issue may lie within.



I want to compare the title portion of my Book objects, in my books ListArray, to the user's input. If anyone could just bump me in the right direction, it would be much appreciated.

 
Adam Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an update of my code in its current state. First the public Book getBook() method:

Here is the public String getTitle(Book book) method:

I still have yet to test it, but I think I'm getting closer!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Farrell wrote:Here is an update of my code in its current state. First the public Book getBook() method:

You do realise that lines 2 and 3 don't really do anything. They both compare 2 items and then ignore the result.
Did you mean them to be part of an if statement ?
 
Adam Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,
I don't see how it is comparing two items. With line 2 I was trying to make an instance of the Book class, named book, equal the indexed book object at "i" in my books array.
With line 3 I was trying to use my getTitle method to separate the title string (My book objects are composed of three strings) from the book object I pass it.
Note: to actually pass the book object to get title I had to change that code to:



However, upon testing it, it does not seem to be actually comparing the two strings. I suspect getTitle(Book book) method is at fault there. I could be wrong. If you'd like I can post a greater amount of the code. At the moment, I am merely posting the parts I am having issue with.
Edit: The if-statement is comparing the userTitle string (input a couple lines earlier in code but not shown) to the bookTitle. The bookTitle I try to change on each increment of my for loop so that it represents the title of the Book object it is examining at the "i" index. I may be going about this completely wrong, but this is the only way I can think of trying to do this search.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Farrell wrote:With line 2 I was trying to make an instance of the Book class, named book, equal the indexed book object at "i" in my books array.

That's not what the equals method does. It compares the two objects (in your case the object that book refers to and the object that books.get(i) returns a reference to) and it returns true if they are equal and false otherwise.
As you don't assign the return value to anything, that line of code has no effect.

To do what you want to do you simply say
Although I suspect this isn't what you want to do. If book refers to the book you are looking for then you should have something like
and you then need to override the equals method in your Book class.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Farrell wrote:With line 3 I was trying to use my getTitle method to separate the title string (My book objects are composed of three strings) from the book object I pass it.
Note: to actually pass the book object to get title I had to change that code to:


There's no need to pass the book variable to the getTitle method. It will return the title of the object you call the method on.
Your getTitle method doesn't need a parameter
 
Adam Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all of your input Joanne!
Although I suspect this isn't what you want to do. If book refers to the book you are looking for then you should have something like
book refers to books in the store. bookTitle is the title that I am trying to use as a search parameter. As it stands, everything I enter in as a search parameter will print my book not found statement. To further test, I entered in a print function to print out the two variables I'm having compared. It seems like my if-statement is being ignored on the first run through. Here is the block of code where I'm positive the bug resides:

Here is one of the test runs:
run:

Would you like to search for a specific book? Yes or No?: yes
What is the title of the book you are looking for?: Siddhartha
Siddhartha Siddhartha
Lolita Siddhartha
Sorry - Book not found
BUILD SUCCESSFUL (total time: 9 seconds)


I apologize for the big post. Is this because I need to override something? I have no idea how to override. However, I am going to look it up now. If you know how to explain it really well, it would be much appreciated.

EDIT: It should be noted that the input lolita or Lolita will return the correct Book object value. I used a blank book object to be the first book, because I thought I had finally figured out the bug... I mean feature! Alas, even with a blank book as the first index, it won't recognize Siddhartha or siddhartha, which it most definitely should.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Farrell wrote:EDIT: It should be noted that the input lolita or Lolita will return the correct Book object value.

Is Lolita the last book in your list ?
You loop through your list and when it finds Siddhartha, it sets index to the current value of i.
BUT it then carries on through the list. The title of the next book doesn't match so index is reset to -1;
It works with Lolita because Lolita is the last book in the list and so there is no next book to reset index to -1.
Try this
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much do you know about data structures (this is a Java Tutorials link)? There is a kind of data structure which can map titles to objects. Just be careful to use an immutable type for the left half of the mapping. I suggest that you can keep the books in the List, but you can link their title to the book in that other sort of data structure. Just remember: if the title ever changes you might never find the book back.
If you are searching on several features, you would find a database does that sort of think very efficiently.
 
Adam Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, Joanne! It would be something that simple that was thwarting me!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!