aspose file tools*
The moose likes Beginning Java and the fly likes Errors Trying To Compare Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Errors Trying To Compare Strings" Watch "Errors Trying To Compare Strings" New topic
Author

Errors Trying To Compare Strings

sabin mash
Greenhorn

Joined: Aug 31, 2010
Posts: 11
Hello friendly Java experts!

I am writing a Library program, in which books can be flagged as borrowed and not in stock. I was getting close to the end, and am now stumped with errors concerning one line of code (I think). The method inStock, is meant to loop through all of the book titles in an array, and if none of the books in the array match the title it was looking for, the "stocked" field is set to false.

I apologize if this is too much code, but I was told I gave not enough code last time, but I will direct you to the lines that matter here. The current output is:

Line 51 seems to be the culprit. I have pondered and reworked this one line for a while and cannot figure out why it is giving me errors. I am sure as usualy the solution is simple, but your help would be greatly appreciated.
Here is the code:


Let me know if I missed any crucial information.
Thank you very much for your time! This forum's generosity is inspiring.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

If borrowBook() works for firstLibrary, why secondLibrary doesn't ?
Do you realize what are the differences between the two?
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

In secondLibrary why you are not adding any book .

Please try to add book
secondLibrary.addBook(new Book("The Lord of the Rings"));
and then borrow.
swapnil kachave
Greenhorn

Joined: Feb 10, 2010
Posts: 27
Please read the concept of Object Array creation and use.
In your example you create the object reference as "secondLibrary" of Liberary class .which contain books as object array which is get initialized with length of 4 . but array is not holding any object of book. Array is only get initialized it is not get filled . that's why you are getting exception while accessing book array of secondLibray.
Check Book Object in book array of secondlibrary you will get your answer by checking value of book[i] with null.
sabin mash
Greenhorn

Joined: Aug 31, 2010
Posts: 11
Ah i see. I was checking null with title, not with the value of book[i], which is what was necessary. It functions as it is supposed to now. Thank you very much!

This assignment is from a java "course" on the MIT Open Courseware site. It asked that secondlibrary how now books inside it, which made things tricky for me. I got confused between whether to make a comparison with the value of book[i], or books[i].title.
Bryan Babon
Greenhorn

Joined: Aug 29, 2012
Posts: 3
Hi to all! can I ask here? How can I update or remove the object that was already borrowed? For example when you print the available books at Library.java:108, the book there "The Lord of the Rings" should be removed since you already borrowed it at Library.java:101. So the output at Library.java:108 would be the three other books without "The Lord of the Rings". Also if you try to return the book at Library.java:116 and print the available books again, "The Lord of the Rings" there should be back. Anyone else please help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
Welcome to the Ranch Bryan Babon.

You need to write the Book class in such a way as to support that functionality. Not as above, where its fields appear not to be private. You need to have a printAvailableBooks() method which iterates the array and prints those which are not already borrowed. That is quite a simple task.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

sabin mash wrote:Hello friendly Java experts!


Let me know if I missed any crucial information.
Thank you very much for your time! This forum's generosity is inspiring.


What if the book is found at the 2nd last position, and is not at the last position.. This loop is designed to return false in that case...
Try modify this loop to solve this issue..
Hint: - Once you are done with any task... You should probably skip doing that task again and again..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
You realise you are answering a two‑year‑old question?
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Campbell Ritchie wrote:You realise you are answering a two‑year‑old question?


Wow. I really didn't saw that as it came in the recent post due to question asked by Bryan Babon. And I realise it now.
Bryan Babon
Greenhorn

Joined: Aug 29, 2012
Posts: 3
Does the position of the book really matter? I thought the loop was ok, but the output was going wrong, when I'm borrowing the book it considers only the book at the last position... you're right Sir R.jain.
I'm sorry I didn't get the hint because I'm a total beginner ;(
Thanks anyway for the reply
How can I modify it? that's the last problem that I can't figure..
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Bryan Babon wrote:Does the position of the book really matter? I thought the loop was ok, but the output was going wrong, when I'm borrowing the book it considers only the book at the last position... you're right Sir R.jain.
I'm sorry I didn't get the hint because I'm a total beginner ;(
Thanks anyway for the reply
How can I modify it? that's the last problem that I can't figure..

Well, I gave a hint in my last post for your problem..
But still, I'll clarify it once again..
In the if block you're are trying to find out whether that book is available or not.. If it is available then you got what you needed..
You don't need to move further in the loop after that.. So, you can just break out of loop, if you found a book (That is inside if, where you're making that boolean variable true.)

This is because, if you continue to run loop after that, then there are bright chances that else will be executed on the next run, and the boolean variable will once again get false..

So, finally if that is false, it means you didn't find any book, though you did get one..
I hope you got your problem and what I want to say
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Bryan Babon wrote:Sir R.jain.

Haha .. Well Mr. Bryan, "Sir" is used before the name of highly dignified people. Those who have achieved something remarkable..
And till now I haven't done any.. That is still far-fetched. Isn't that??
Bryan Babon
Greenhorn

Joined: Aug 29, 2012
Posts: 3
thanks for the reply, I can understand it now
and I called you sir because that's what I want to call to a person who is good in JAVA thanks anyway ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Errors Trying To Compare Strings