Search code examples
javaarrayssearchbinary-searchlinear-search

Applying Linear and Binary Searches to Arrays


I have to create a program that takes a user input (a number) and then the program should have that number and apply a search to the array and output the corresponding title by matching the index and the number the user inputted. However during run time, nothing happens. I have set breakers in my code and noticed a problem with the for loop (search algorithm). Please help me and let me know what is wrong is my search algorithm. What I am trying to do is use the number of that the user inputs to match a index and then output the book title that is stored in the index.

       private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:  

    // declares an array 
   String[] listOfBooks = new String [101];

   // assigns index in array to book title 
   listOfBooks[1] = "The Adventures of Tom Sawyer"; 
   listOfBooks[2] = "Huckleberry Finn"; 
   listOfBooks[4] = "The Sword in the Stone";
   listOfBooks[6] = "Stuart Little";
   listOfBooks[10] = "Treasure Island";
   listOfBooks[12] = "Test";
   listOfBooks[14] = "Alice's Adventures in Wonderland";
   listOfBooks[20] = "Twenty Thousand Leagues Under the Sea";
   listOfBooks[24] = "Peter Pan";
   listOfBooks[26] = "Charlotte's Web";
   listOfBooks[31] = "A Little Princess";
   listOfBooks[32] = "Little Women";
   listOfBooks[33] = "Black Beauty";
   listOfBooks[35] = "The Merry Adventures of Robin Hood";
   listOfBooks[40] = "Robinson Crusoe";
   listOfBooks[46] = "Anne of Green Gables";
   listOfBooks[50] = "Little House in the Big Woods";
   listOfBooks[52] = "Swiss Family Robinson";
   listOfBooks[54] = "The Lion, the Witch and the Wardrobe";
   listOfBooks[54] = "Heidi";
   listOfBooks[66] = "A Winkle in Time";
   listOfBooks[100] = "Mary Poppins";

    // gets user input 
    String numberInput = txtNumberInput.getText();
    int number = Integer.parseInt(numberInput);

    // Linear search to match index number  and user input number
        for(int i = 0; i < listOfBooks.length - 1; i++) {
        if (listOfBooks.get(i) == number) {
        txtLinearOutput.setText(listOfBooks[i]);
        break; 
        }


    }

*There is a problem with the listOfBooks.get in the if statement. Also I need to apply a binary search that would search the same array just using the binary method. Need help to apply this type of binary search.

How could I make a statement that checks if the int number is equal to an index?

Note that the following code is just an example of what I have to apply. Variables are all for example purposes:

public static Boolean binarySearch(String [ ] A, int left, int right, String V){
     int middle;

     if (left > right) {
         return false;
     }

     middle = (left + right)/2;
     int compare = V.compareTo(A[middle]);
     if (compare == 0) {
         return true;
     }
     if (compare < 0) {
         return binarySearch(A, left, middle-1, V);
     } else {
         return binarySearch(A, middle + 1, right, V);
     }
 }

Solution

  • you can avoid for loop and check condition by just giving number like this: txtLinearOutput.setText(listOfBooks[number-1]);

    remove your code

    // Linear search to match index number  and user input number
    for(int i = 0; i < listOfBooks.length - 1; i++) {
        if (listOfBooks.get(i) == number) {
         txtLinearOutput.setText(listOfBooks[i]);
         break; 
    }
    

    with

    try{
         int number = Integer.parseInt(numberInput);
         if(number>0 && number<101){
           txtLinearOutput.setText(listOfBooks[number-1]);
         }else{
            // out of range
         }
     }catch(Exception e){
       // handle exception here
     }