-3

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!");

            }

    }
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
hooman
  • 23
  • 5

2 Answers2

3

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]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You are comparing the whole array to a single string, that will always return false.

It's the same as:

  String[] names = {"a", "b", "c"};
  names.equals("d");

Iterate the array to see if there string is there

 int i = 0;
 for (String item: names) {
     if (item.equals(inName) ) {
         return i;
     }
     i++
  }
  if (i == names.length ) {
    // not found 
  }

Running example:

public class A {
  public static void main(String...args){
    String[] names = {"a", "b", "c"};
    String inName = "d";

     int i = 0;
     for (String item: names) {
      if (item.equals(inName) ) {
        System.out.println(i);
        break;
         //return i;
      }
      i++;
    }
    if (i == names.length ) {
       System.out.println(-1);
        // not found
    }
  }
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569