【问题标题】:I want to find the index of the string in an array我想在数组中找到字符串的索引
【发布时间】:2019-12-19 21:28:04
【问题描述】:

我必须从用户输入的数组中搜索一个字符串,但我的逻辑有错误。即使用户输入在数组中,我仍然会得到“找不到数据”

我还必须显示字符串在数组中的索引,如果它被发现但那里也有错误。

以下是我尝试过的代码。

这是最初的问题

  1. 创建一个要求用户插入 5 个名称的程序。
  2. 在数组中存储名称
  3. 要求用户插入他们想从之前创建的列表中找到的名称
  4. 如果找到名称,则显示“在 [index] 找到的数据”
  5. 如果没有,则显示“未找到数据”。暗示;使用 Java 方法 equals 比较两个字符串。
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!");

            }

    }
}

【问题讨论】:

  • 你得到什么错误?
  • 你正在测试一个字符串数组是否等于一个字符串,这没有逻辑意义。
  • 当参数是字符串时,为什么你认为 array.equals 会返回除了 false 之外的任何东西?
  • 在``` names.equals(inName) ``` 行中,names 是Array 类型,需要将names 中的元素与inName 进行比较。您可以再次遍历数组并将数组的每个元素与 inName 进行比较

标签: java arrays algorithm indexof string-search


【解决方案1】:

您需要将inName 的值与存储在数组中的每个值进行比较,而不是与数组本身进行比较。您可以使用以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!");
}

完整程序:

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

示例运行:

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]

【讨论】:

    【解决方案2】:

    您将整个数组与单个字符串进行比较,该字符串将始终返回 false。

    同理:

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

    遍历数组看是否有字符串

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

    运行示例:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多