Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Incompatible Types java.lang.String

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I am working on a project right now to create a simple library system. I am trying to compile, but I am getting "Incompatible Types; Found : Book; Required java.lang.string" It is on the lines that return b in the available books constructor.

Here is the code I have so far:



Also, my goal with the availableBooks constructor is to get it to where the book doesn't appear on the list of available books when it is borrowed. How could I rearrange my constructor to accomplish this?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to use a List<Book> instead; now you're limited to the size you specify when you create the array. Which you don't, by the way. books remains null.

I think you want to swap that assignment around. At this point, address is null. You are then assigning null to the local variable, overwriting its previous value.

What is the type of b? And what is the required return type? Those two do not match, do they?
 
Abhijeet Ravankar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type of avalibleBooks(Book b) function is "String". While you are returning b, where b is of type Book and NOT String.

Try changing the return type to Book:



 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have another serious error.

Never write == true. You simply write if (b) . . .

Never write == false. You simply write if (!b) . . .

Those forms with == are both poor style, and you can get all sorts of nasty errors if you mistakenly write = instead of ==. You do in fact appear to have written = instead of == in at least one place.
Also you ought to give the borrowed attribute private access, then you can access it with an isBorrowed method. You ought not to get direct access to fields of an object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic