Home > database >  Java array linear search always returns "Book is not found"?
Java array linear search always returns "Book is not found"?

Time:04-05

When I insert the arguments the search always returns "not found" - even though the searched value was input into the array?

import java.util.Scanner;
public class assignment {
  public static void main (String[]   args) 
  {
     Scanner sc = new Scanner(System.in);
     
     String searchValue = "";
     String [] BookID = new String [3];
     String [] Booktitle = new String [3];

//input the BookID    
     System.out.println("Enter the 12 BookID");
     for (int a = 0 ; a < BookID.length; a  )
        {
        System.out.print("BookID :");
        BookID[a] = sc.next();
        }
        
//Input the book title        
     for (int b = 0 ; b < Booktitle.length ; b  )
        {
        System.out.print("Booktitle :");
        Booktitle[b] = sc.next();
        }

//The Linear search on BookID
     System.out.print("Enter BookID to find :");
     for(int c = 0; c < BookID.length; c  )
        {
        searchValue = sc.next();
        if(searchValue.equals(BookID))
            System.out.print("BookID is found : ");
        else 
            System.out.print("BookID is not found : ");
        }
        
}
}
     
     

I'm expecting the result to return like so: if input BookID 112. The Linear search would return "The BookID is found :" instead of the else statement.

CodePudding user response:

Try printing out the value of the bookId you wanna find to see if there anything with the string that could cause it to not be equals. Also, you could convert the string to an integer with:

Integer.parseInt("BookId");

The equals would have less chance of failing, you could also change de the array for an array of int instead of String.

CodePudding user response:

This code has some basic things right, but it could be a bit better. Some of the changes I made are for keeping Java conventions (so the code is easier to read and understand) and some are functional. I added them as comments.

import java.util.Scanner;
public class Assignment {
  public static void main (String[] args) 
  {
     Scanner sc = new Scanner(System.in);
     
     String searchValue = "";
     String [] bookIDs = new String [3]; //lowercase for the attribute name (convention)
     String [] bookTitles = new String [3]; //plural for the array (convention)

     //input the BookID    
     System.out.println("Enter the Book ID");
     for (int i = 0 ; i < bookIDs.length; i  ) { //you can re-use the i in all loops
        System.out.print("Enter "   i   " Book ID: ");
        bookIDs[i] = sc.next();
     }
        
     //Input the book title        
     for (int i = 0 ; i < bookTitles.length ; i  ) {
        System.out.print("Enter "   i   " Book title: ");
        bookTitles[i] = sc.next();
     }

     //The Linear search on BookID
     System.out.print("Enter Book ID to find: ");
     searchValue = sc.next(); //NOTE: this is your first mistake. read out of the loop
     for(int i = 0; i < bookIDs.length; i  ) {
        if(searchValue.equals(bookIDs[i])) { //NOTE: this is your second mistake - you wanted the array value, not the array
            System.out.println("BookID is found in position " i);
            break;  //suggestion - stop the loop when found.
        }
        else {
            System.out.println("BookID is not found in position " i);
        }
     }
     sc.close();  //NOTE: important to close scanners at the end.
  }
}

Good luck with your studies.

  • Related