+ 3
Can someone please solve this issue?
I have make a program which have 1 class Library which have 2 properties and some methods to add books, return books, show available books. Program compiles with no error but one method "issueBook" is not working properly. It is giving wrong output. Please help me out... https://code.sololearn.com/cmHtlcJj4osp/?ref=app
16 Réponses
+ 1
Remove line 9. As I wrote earlier, "In issueBook() add a boolean variable ..."
You also need to print the value returned by east.availableBooks() at line 80, just as you did at line 69.
public void issueBook(String book)
{
boolean anyMatch = false;
for(int i = 0; i < this.books.length; i++)
{
if(this.books[i] != null && this.books[i].equals(book))
{
System.out.println(book + " has been issued!");
this.books[i] = null;
no_of_books--;
anyMatch = true;
break;
}
}
if(!anyMatch)
{
System.out.println(book + " is not available!");
}
}
Edited to cope with null string array element(s).
+ 3
Ipang Thanks a lot ipang finally it runs with zero errors and bugs. It can't be possible without your help 🤗
https://code.sololearn.com/cNdcFpGCYDz5/?ref=app
+ 1
Ipang see after adding a boolean variable, it gives an error.
+ 1
Ipang thanks a lot Ipang almost it is fixed but when I try to run issueBook method more than once, it gives a error
https://code.sololearn.com/c2fuypbSEgu6/?ref=app
+ 1
That's because .equals() method isn't supposed to be working with a null reference. So when books[ i ] is null, .equals() fails.
Change line 22 to add a check for non-null string object before calling its .equals() method
if(this.books[i] != null && this.books[i].equals(book))
+ 1
For next time please, make changes in another code bit and leave original code intact. This is so that people who visit this post later on can relate what changes were made to make the code work as it should : )
+ 1
Ipang see it is still not working after changing line 22
https://code.sololearn.com/cFVEJ9cYJPDk/?ref=app
+ 1
Bro take a closer look. It's like this
if( this.books[ i ] != null && this.books[ i ].equals( book ) )
You currently don't have [ i ] at line 22 for checking null
if(this.books != null && this.books[i].equals(book))
+ 1
Your book store has some logical error
That is
Suppose I have added books
A, B, C, D
at 0, 1, 2, 3
and the length will 4
If I issued C
then
A, B, Null , D
0, 1, 2, 3
and the length will be 3
If i want to add a book E
Then
A, B, Null , D, E
0, 1, 2 , 3, 4
To solve this few ways are there
1. To keep move the null to right
2. Fill the new book in null first
+ 1
Konda Sandeep thanks a lot bro for your help. I really appreciate it.
The issue is already resolved by Ipang
+ 1
It's okay bro
0
See when you run the issueBook() method, it runs both if and else conditions inside the method for a single input. And when I try to run issueBook() method more than once, it also gives error.
Please help me to fix that.
0
Removed line 23 but it does not works. It still runs both if and else cases for a single input.
0
In issueBook(), add a boolean variable e.g. <anyMatch> to indicate a book was found to match the query (book title given as argument). Initialize <anyMatch> by false.
Inside for.loop, set <anyMatch> to true when a book matches, before breaking out of the loop.
Remove the `else` block entirely.
After loop complete, do check whether <anyMatch> is still false. If it is (meaning no book matched the query), then you print "<book> is not available!"
0
User42 sorry feeling sleepy, cause it's 10 pm here that's why I declared it at wrong Place 😅