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

            }

    }
}
4
  • 1
    What errors do you get? Commented Dec 19, 2019 at 21:30
  • You're testing if a String array equals a String, which doesn't make logical sense. Commented Dec 19, 2019 at 21:30
  • Why do you think array.equals will ever return anything but false when the argument is a string? Commented Dec 19, 2019 at 21:31
  • In the line ``` names.equals(inName) ``` names is of Array type, you need to compare the elements inside names with inName. You can again iterate over the array and compare each element of the array with inName Commented Dec 19, 2019 at 21:34

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.