简体   繁体   English

如何在Java中找到数组中字符串的索引号

[英]How do you find the index number of a string in an array in Java

I have no idea if I'm coding this efficiently, or even correctly, but I want to input a name, address, and phone number. 我不知道我是否在有效地编码甚至正确编码,但是我想输入姓名,地址和电话号码。 I then want to have input find a match from the input array, and use that same index number to print the corresponding information. 然后,我想让输入从输入数组中找到一个匹配项,并使用相同的索引号来打印相应的信息。

import java.util.*;

public class NameAddress {

    public static void main(String[] args) {

        Scanner ui = new Scanner(System.in);
        System.out.println("Welcome to the name collecting database");
        String []names = new String[5];
        String []address = new String[5];
        String []phone = new String [5];
        int count =0;

        while (count<=5)
        {
            System.out.println("Please enter the name you would like to input");
            names[count] =ui.next();
            System.out.println("Name has been registered into Slot "+(count+1)+" :"+Arrays.toString(names));
            System.out.println("Please enter the address corresponding with this name");
            ui.nextLine();
            address[count] = ui.nextLine();
            System.out.println(names[count]+" has inputted the address: "+address[count]+"\nPlease input your phone number");
            phone[count]=ui.nextLine();
            System.out.println(names[count]+"'s phone number is: "+phone[count]+"\nWould you like to add a new user? (Yes or No)");

            if (ui.next().equals("No"))
            {
                System.out.println("Please enter a name to see matched information");
                String name = ui.next();
                if(name.equals(names[count]))
                {
                    System.out.println("Name: "+names[count]+"\nAddress: "+address[count]+"\nPhone: "+phone[count]);
                }
                count=6;
            }
            count++;
        } 
    }

}
        System.out.println("Please enter a name to see matched information");
        String name = ui.next();
        for(int i = 0; i <names.length;i++){
        if(name.equals(names[i]))
        {
            System.out.println("Name: "+names[i]+"\nAddress: "+address[i]+"\nPhone: "+phone[i]);
        }
        }

if(name.equals(names[count])) will work only if the name user is searching is at the current index of names . if(name.equals(names[count]))将工作仅当name用户搜索的是在当前索引names So you have to check every item in the array to determine whether it exists in the array. 因此,您必须检查数组中的每个项目,以确定它是否存在于数组中。 You can do this: 你可以这样做:

int itemIndex = Arrays.asList(names).indexOf(name);
if(itemIndex>=0) // instead of if(name.equals(names[count]))
{
    // rest of the codes; use the itemIndex to retrieve other information
}
else
{
    System.out.println(name + " was not found");
}

Or manually loop over the names array as others showed. 或者像其他人一样手动遍历names数组。

It seems like you have data input already done. 似乎您已经完成了数据输入。

As for data retrieval by searching, if you don't care about efficiency, then you could iterate over the entire array to see if the text inputted matches any element in your array using 至于通过搜索进行数据检索,如果您不关心效率,则可以遍历整个数组,以查看输入的文本是否与您使用的数组中的任何元素匹配

int searchIndex = 0;
for (int i = 0; i < names.length; i++) {
    if (searchString.equals(names[i])) {
        searchIndex = i;
    }
}

where searchString would be the string input by the user to find the element in the array. 其中searchString是用户输入的用于在数组中查找元素的字符串。 The code block above assumes you don't have duplicate data, but you could easily tweak the returned index to contain an array of indexes that contain your data, if you wish. 上面的代码块假定您没有重复的数据,但是您可以根据需要轻松地调整返回的索引以包含包含数据的索引数组。 You'd then have an index number (or index numbers) with which you could use to find the rest of the data in your other arrays. 然后,您将拥有一个索引号(或多个索引号),可用于查找其他数组中的其余数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM