Java 我想在数组中找到字符串的索引
我必须从用户输入中搜索数组中的字符串,但我的逻辑中有一个错误。即使用户输入在数组中,我仍然会得到“未找到数据” 我还必须显示字符串在数组中所处的索引,如果它被找到了,但也出现了错误 下面是我尝试过的代码 这是最初的问题Java 我想在数组中找到字符串的索引,java,arrays,algorithm,indexof,string-search,Java,Arrays,Algorithm,Indexof,String Search,我必须从用户输入中搜索数组中的字符串,但我的逻辑中有一个错误。即使用户输入在数组中,我仍然会得到“未找到数据” 我还必须显示字符串在数组中所处的索引,如果它被找到了,但也出现了错误 下面是我尝试过的代码 这是最初的问题 创建一个程序,要求用户插入5个名称 在数组中存储名称 要求用户插入他们希望从先前创建的列表中查找的名称 如果找到名称,则显示“在[索引]处找到的数据” 如果未找到,则显示“未找到数据”。暗示使用Java方法equals比较两个字符串 package-stringsearch; 导
package-stringsearch;
导入java.util.Scanner;
公共类字符串搜索
{
公共静态void main(字符串[]args)
{
int i;
扫描仪sc=新的扫描仪(System.in);
字符串[]名称=新字符串[5];
对于(i=0;i”;
名称[i]=sc.nextLine();
}
系统输出打印(“输入要比较的名称>”;
字符串inName=sc.nextLine();
if(name.equals(inName)){
System.out.println(“在[“+i+”]处找到的数据”);
}
其他的
{
System.out.println(“未找到数据!”);
}
}
}
将整个数组与单个字符串进行比较,该字符串将始终返回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
}
}
}
您需要将名称中的
值与数组中存储的每个值进行比较,而不是与数组本身进行比较。您可以使用从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!");
}
您会遇到什么错误?您正在测试字符串数组是否等于字符串,这在逻辑上是没有意义的。为什么您认为当参数是字符串时,array.equals将返回除false以外的任何值?在```names.equals(inName)``行中,names是数组类型,您需要比较names和inName中的元素。您可以再次迭代数组,并将数组的每个元素与inName进行比较
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]