我想在数组中找到字符串的索引
I want to find the index of the string in an array
我必须从用户输入的数组中搜索字符串,但我的逻辑有错误。即使用户输入在数组中,我仍然得到 "data not found"
我还必须显示字符串在数组中的索引(如果找到但也有错误)。
下面是我试过的代码。
这是最初的问题
- create a program that ask user to insert 5 names.
- store names in array
- ask user to insert the name they want to find from the list created earlier
- if name found, display "data found at [index]"
- 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!");
}
}
}
您正在将整个数组与单个字符串进行比较,这将始终 return 错误。
等于:
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
}
}
}
您需要将 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]
我必须从用户输入的数组中搜索字符串,但我的逻辑有错误。即使用户输入在数组中,我仍然得到 "data not found"
我还必须显示字符串在数组中的索引(如果找到但也有错误)。
下面是我试过的代码。
这是最初的问题
- create a program that ask user to insert 5 names.
- store names in array
- ask user to insert the name they want to find from the list created earlier
- if name found, display "data found at [index]"
- 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!");
}
}
}
您正在将整个数组与单个字符串进行比较,这将始终 return 错误。
等于:
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
}
}
}
您需要将 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]