• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help searching objects that have multiple states within an arrayList  RSS feed

 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have set up a Book object and an arrayList And And

I am trying to set up a search function where the user inputs the bookID into the scanner and the program searches through the arrayList to find and display the corresponding book and its details.

I have tried if statements like this:

But I can't get it to work. Any suggestions?
Thanks.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you overridden the equals() method in the book class? The usual method you want to find things an a List (this method) depends on a correct implementation of equals.

Why do you still have the loan count and whether on loan as constructor parameters? Surely those have default values for all instances?

Your test with if (scanner.equals(…)) will compile because Scanners (like everything else) have equals methods, but you can rely on that particular formula always returning false. That formula looks as if you were guessing. Now you can guess, maybe 1000000× and if your guesses are all different there is a good chance one will work. Or you can think about it an it will work first time. Start by explaining what you are using to define the book equal (or at least found) for the purposes of this exercise.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What "doesn't work"? Could you describe it more thoroughly? Could you post all the relevant code?
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What "doesn't work"? Could you describe it more thoroughly? Could you post all the relevant code?


I can't get the program to find a match. For example, say there is a book with a bookID of 1, and the user wants to find the details of the book with the bookID of 1 by entering 1 into the console.

Campbell Ritchie wrote:How have you overridden the equals() method in the book class? The usual method you want to find things an a List (this method) depends on a correct implementation of equals.


Well this is all new to me, we have not covered this sort of thing in my lectures yet, but I thought I would give it a try. I have not tried to override the equals() method. Is there any way to do it without a List?
Campbell Ritchie wrote:Why do you still have the loan count and whether on loan as constructor parameters? Surely those have default values for all instances?

Yes, they all have default values, just not in that particular section of code.

Something like this? I take in the user input for the first three states and then I have methods eg. to take a book on loan which sets the onLoan value to true and increments the number of times loaned.
Then I add a new book to the array.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, please stop for now. In previous your thread we all discussed about quite many things needed to be fixed before you move ahead. It appears you haven't fixed non of them, which lead your program look like a Whac-A-Mole game, instead, just errors popping up.

1. Why don't you start from the instance variables. BookID for instance, apart from non conventional name, where b suppose to be a lower case, where do you get this ID from? Shouldn't be something a global variable, where it increments automatically during new book release?

2. Once again, get decent amount of parameters in your constructor - probably no more than two.

Probably it is enough to write and see if it is compiles. Later add some setter/getter methods and see if you can get those values be set, and later returned.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Ok, please stop for now. In previous your thread we all discussed about quite many things needed to be fixed before you move ahead. It appears you haven't fixed non of them, which lead your program look like a Whac-A-Mole game, instead, just errors popping up.

1. Why don't you start from the instance variables. BookID for instance, apart from non conventional name, where b suppose to be a lower case, where do you get this ID from? Shouldn't be something a global variable, where it increments automatically during new book release?

2. Once again, get decent amount of parameters in your constructor - probably no more than two.

Probably it is enough to write and see if it is compiles. Later add some setter/getter methods and see if you can get those values be set, and later returned.

I actually have no errors in my program, it works just how I want it to, apart from this search feature which I can take or leave, it doesn't really matter. Ok, yes I agree that should be lower case, rookie mistake. On my menu, the first option is to add a book. The user inputs the bookID, title, and author. onLoan is set to false by default and the numTimesLoaned is 0 by default. These are all in my constructor and I have all the relevant accessors and mutators. I don't know if this is how people code it in the real world, but this is how I've been taught it for the past 2-3 months.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What "doesn't work"? Could you describe it more thoroughly? Could you post all the relevant code?

Oh, and, I'd like to post all of my code now, but this is for an assignment, so I don't want it all to be available online until after it has been marked, just to be safe.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Knight wrote:On my menu, the first option is to add a book. The user inputs the bookID, title, and author.
Ok, fair enough then, of course you should be following assignment requirements. Sorry if I sounded dissagreeing.

I would just think, how the user knows during the book creation, that such ID doesn't exist already, which ID should be the net one... Unless it is ISBN. Anyways, since the bookID is a primitive type, you can simply iterate throught books list and look for that value by getting it with your getter method and comparing with == sign with your searchable ID. If I understood your problem correctly.



 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's ok I understand what you meant, I was quite vague. Yeah I don't think we have learnt code to stop the user entering the same ID more than once, unless its easy and you care to enlighten me .
So would something like this work:
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since it is an assignment, I'd read requirements once again very carefully, to make sure you correctly understand them. I'm not sure about this ID whether you need to check if such exist already or not. Maybe you're not required to do so, maybe you're given those ID's of each book - I don't know that, you'll have to decide your implementation.

But from what you wrote, you should not have more than 3 parameters in your constructor.
Samuel Knight wrote:inputs the bookID, title, and author

So, this:
Suppose to become similar to (as per your requirements, unless there is something else. You haven't mentioned about typeOfBook nowhere, so double check it for yourself if you need it):

Also, if I understood you correctly, based on provided ID, you suppose to find that book and provide all information about it, so, based on that fact, consider overriding toString method, where you could print out needed information about the book instance.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Knight wrote:. . .Yes, they all have default values, just not in that particular section of code.

Something like this? . . .
Sort of. There are fields whose values will differ from book to book and fields whose values will (at least initially) be the same for all books. The author title and type will vary but every book will start off life not being lent. Those fields which are different need corresponding constructor parameters and those fields which are the same for all instances don't. Those which are the same for all instances can be instantiated with the declaration or in the constructor. Those which are different must be instantiated from constructor parameters. There is no point in initialising author to "" or ID to 0.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Well, since it is an assignment, I'd read requirements once again very carefully, to make sure you correctly understand them. I'm not sure about this ID whether you need to check if such exist already or not. Maybe you're not required to do so, maybe you're given those ID's of each book - I don't know that, you'll have to decide your implementation.

But from what you wrote, you should not have more than 3 parameters in your constructor.
Samuel Knight wrote:inputs the bookID, title, and author

So, this:
Suppose to become similar to (as per your requirements, unless there is something else. You haven't mentioned about typeOfBook nowhere, so double check it for yourself if you need it):

Also, if I understood you correctly, based on provided ID, you suppose to find that book and provide all information about it, so, based on that fact, consider overriding toString method, where you could print out needed information about the book instance.

The assignment says to have appropriate validation, which is quite ambiguous. From what I can gather, the way I have done it is the way that it should be, even though it doesn't make the most sense.

Oh I forgot about the typeOfBook. This is just a String whether it is a hardback or paperback book.

Why should I leave out the onLoan and numOfLoans in my constructor? In this instance, for onLoan and numOfLoans I had to put etc. keyword being this.

Yes you have understood correctly. The toString method sounds promising, I just have trouble actually checking and getting the bookID to see if it is equal to the userInput.

Thanks
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

'contains()' returns a boolean, T|F if it is found, why are you comparing that to scanner?
getBookID() should be returning an int but you are searching an ArrayList of Book's.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Knight wrote:The assignment says to have appropriate validation, which is quite ambiguous. From what I can gather, the way I have done it is the way that it should be
Indeed, could explain also what do they consider as "appropriate". By the way, have you restricted user from entering ID with - sign? i.e. -44.

Samuel Knight wrote:Why should I leave out the onLoan and numOfLoans in my constructor?
What Campbell said, that you should not be leaving this as constructor parameter. You can initialize it in constructor or outside with initial value, but do not accept those values as arguments in constructor. Give them initial values to false (onLoan) and 0 (numOfLoans).

Samuel Knight wrote:I had to put "this.onLoan = onLoan"
No, don't do that. Just keep in that way

 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
'contains()' returns a boolean, T|F if it is found, why are you comparing that to scanner?
getBookID() should be returning an int but you are searching an ArrayList of Book's.

Honestly I have no idea about it . I Googled it and things like that came up. Do you have any ideas how I could write it?
Thanks
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Samuel Knight wrote:The assignment says to have appropriate validation, which is quite ambiguous. From what I can gather, the way I have done it is the way that it should be
Indeed, could explain also what do they consider as "appropriate". By the way, have you restricted user from entering ID with - sign? i.e. -44.

Samuel Knight wrote:Why should I leave out the onLoan and numOfLoans in my constructor?
What Campbell said, that you should not be leaving this as constructor parameter. You can initialize it in constructor or outside with initial value, but do not accept those values as arguments in constructor. Give them initial values to false (onLoan) and 0 (numOfLoans).

Samuel Knight wrote:I had to put "this.onLoan = onLoan"
No, don't do that. Just keep in that way



They did not specify what they consider as appropriate. My best guess would be to restrict the user to only entering integers or only strings, depending on what they are entering, and also making sure that they can only select items in the menu etc.

I have just checked and no, I have not restricted that, I wouldn't know how to go about it. Would I just check if they enter in a '-' and if they do, show an error message to prompt them to enter again?

Ah, okay, that makes sense now, thanks.

Any idea on what code I should use to actually find and get the bookID, and then show the user the details?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Knight wrote:
Carey Brown wrote:
'contains()' returns a boolean, T|F if it is found, why are you comparing that to scanner?
getBookID() should be returning an int but you are searching an ArrayList of Book's.

Honestly I have no idea about it . I Googled it and things like that came up. Do you have any ideas how I could write it?
Thanks

You'll need to provide an equals method as Campbell mentioned.
Campbell Ritchie wrote:How have you overridden the equals() method in the book class?

You'll need a Book constructor that only takes an id.
Use scanner to input an id.
Create a dummy Book with that id.
Use ArrayList.indexOf() to find the Book in the bookList using the dummy Book as the search criteria (check for -1).
Use ArrayList.get() to get the found Book.

Alternatively you could bypass indexOf() and get() and loop over the list yourself looking for a Book with the specified id. This would also eliminate the need for a dummy Book or the equals() method.


 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked what I read about the Boolean not being comparable to a scanner object. I think the right step is to use scanner.next() and compare that value to the bookList array element in a for loop. I'm trying to think of the problem in it's simplest form: One bookID at a time. Then that can be scaled up using a for loop.



Also, the loanStatus, numOfLoans and typeOfBook seem like they need a method extraction as was previously mentioned. But I am a lowly greenhorn after all!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Knight wrote:. . . I Googled it and things like that came up. . . .
Do tell us where … so we know to avoid that site

If you want to find a book, you need to compare something. There is something in the book and something outside. Are you simply searching the books for ID? If so, turn all computers in sight off and get a paper pencil and eraser (the last is the most important piece of hardware at the moment). Write down how you are going to find a book from its ID. If any words look anything like Java® or any other sort of code, delete them and replace them with plain simple English. It should be simple enough that Fred Rosenberger's proverbial ten‑year‑old can understand it. Or my four‑year‑old grandson.

Alternatively find a ten‑year‑old and get him to explain it
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you had here is going a long way in the right direction. I would enhance it like this:-I added an extra pair of {} to limit the scope of found. If you read the addition in line 5 as, “and not found,” it should be easier to understand. Note that loop is not complete; you must do something else to get your hands on the book object.

Please indent your code consistently; I had to add indentation to that code which I copied from your earlier post, and also spaces before the {.
Don't create lots of Scanner objects pointing to System.in. You only ever need one Scanner pointing to System.in and you use can it throughout your program. Never close it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!