javaarraysalgorithmindexofstring-search

I want to find the index of the string in an array


I have to search a string in an array from the user input, but I have an error in my logic. Even when the user input is in the array I still get "data not found"

I also have to display the index where the string is located in the array if it's found but got an error there too.

Below is the code I've tried.

This was the original question

  1. create a program that ask user to insert 5 names.
  2. store names in array
  3. ask user to insert the name they want to find from the list created earlier
  4. if name found, display "data found at [index]"
  5. if not, display "data not found". Hint; use Java method equals to compare two strings.
package stringsearch;
import java.util.Scanner;

public class StringSearch 
{
    public static void main(String[] args)
    {
        int i;
        Scanner sc = new Scanner(System.in);

        String [] names = new String[5];

        for (i = 0; i < names.length; i++) 
        {
            System.out.print("Enter name " + (i + 1) + " > ");

            names[i] = sc.nextLine();
        }

        System.out.print("Input Name to compare > ");
        String inName = sc.nextLine();

            if (names.equals(inName)){

              System.out.println("Data found at ["+i+"]");

            }

            else 
            {

              System.out.println("Data not found!");

            }

    }
}

Solution

  • You need to compare the value of inName with each of the values stored in the array, not with the array itself. You access each of the values stored in the array using the index starting with 0.

    for (i = 0; i < names.length; i++) {
        if (inName.equals(names[i])) {
            System.out.println("Data found at [" + i + "]");
            break;
        }
    }
    
    // If the value stored in `inName` is found, the value of `i` will not reach up
    // to the value equal to `names.length` because of the `break` statement. If the
    // value of `i` has reached there, it means that the value stored in `inName`
    // has not been found.
    if (i == names.length) {
        System.out.println("Data not found!");
    }
    

    Complete program:

    import java.util.Scanner;
    
    public class StringSearch {
        public static void main(String[] args) {
            int i;
            Scanner sc = new Scanner(System.in);
            String[] names = new String[5];
    
            for (i = 0; i < names.length; i++) {
                System.out.print("Enter name " + (i + 1) + " > ");
                names[i] = sc.nextLine();
            }
    
            System.out.print("Input Name to compare > ");
            String inName = sc.nextLine();
    
            for (i = 0; i < names.length; i++) {
                if (inName.equals(names[i])) {
                    System.out.println("Data found at [" + i + "]");
                    break;
                }
            }
    
            // If the value stored in `inName` is found, the value of `i` will not reach up
            // to the value equal to `names.length` because of the `break` statement. If the
            // value of `i` has reached there, it means that the value stored in `inName`
            // has not been found.
            if (i == names.length) {
                System.out.println("Data not found!");
            }
        }
    }
    

    A sample run:

    Enter name 1 > John
    Enter name 2 > Harry
    Enter name 3 > Sam
    Enter name 4 > Cristina
    Enter name 5 > Manny
    Input Name to compare > Sam
    Data found at [2]