Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My school gave me a project to create a BlueJ program for an e library. I was running a linear search and although the compiler doesn't show any error, the program doesn't execute after taking the input for "booksearch" (in the excerpt below). Basically, after taking the name of the book taht is to be searched for, no display of present or absent is there. The program abruptly terminates. What to do?

What I have tried:

...
System.out.println("Enter the book to be searched: ");
String booksearch= sc.nextLine();
int j;
for(j=0;j<10;j++)
{
if (booksearch==book[j])
System.out.println("The book is present at " + j);
break;
}
if(j==10)
System.out.println("Book not found");
...
Posted
Updated 30-May-18 18:35pm

1 solution

I see multiple problem in this.
1.
if (booksearch==book[j])
should be
if (booksearch.equals(book[j]))

2. wrong break statement, it should be
if (booksearch==book[j]) {
        System.out.println("The book is present at " + j);
        break;
}

3. don't know how you initialized book array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900