字符串数组名称搜索

roc*_*ana 6 java file java.util.scanner

所以我有一个文件,其中包含所有的总统 - 他们的名字,中间名首字母(如果有的话)和姓氏.

需要读入该文件,并且用户可以输入总统的名称来搜索它,并且应该显示该总统.

如果用户按名字或姓氏搜索,而不是两者都搜索,我会显示总统.

例如,外部文件包含:

George,Washington,(1789-1797)
Franklin,D.,Roosevelt,(1933-1945)
... and so on with all the presidents
Run Code Online (Sandbox Code Playgroud)

我需要用户能够键入名字,姓氏,或者姓名和名字,并获得所需的结果(日期与大部分时间无关).

试过很多不同的事情,但如果用户按名字和姓氏搜索,就不能到达显示总统的地方.

这是我到目前为止所得到的:

public class NameSearch {

    public static void main(String[] args) throws IOException {

        try {
            // read from presidents file
            Scanner presidentsFile = new Scanner(new File("Presidents.txt"));
            // scanner for user input
            Scanner keyboard = new Scanner(System.in);
            // create array list of each line in presidents file
            ArrayList<String> presidentsArrayList = new ArrayList<String>();

            // prompt user to enter a string to see if it matches with a president's name
            System.out.println("Enter a search string of letters to find a president match: ");
            // store user input
            String userInput = keyboard.nextLine();

            // add president file info to array list linesInPresidentFile
            while (presidentsFile.hasNextLine()) {
                presidentsArrayList.add(presidentsFile.nextLine());
            } // end while loop

            String presidentNamesArray[] = presidentsArrayList.toArray(new String[presidentsArrayList.size()]);
            String results = searchArray(presidentNamesArray, userInput);

            //System.out.println("\nThe presidents who have \"" + userInput + "\" as part of their name are: ");

        } catch (FileNotFoundException ex) {
            // print out error (if any) to screen
            System.out.println(ex.toString());

        } // end catch block

    } // end main

    // method to search for a specific value in an array
    public static String searchArray(String array[], String value) {

        for (int i = 0; i < array.length; i++) {
            if (array[i].toLowerCase().contains(value.toLowerCase())) {
                String splitter[] = array[i].split(" ,");
                System.out.println(Arrays.toString(splitter));
            }

        }
        return Arrays.toString(array);
    }

}
Run Code Online (Sandbox Code Playgroud)

Rub*_*bal 2

我可以通过另一种方式来实现这一点。读取文件输入并将它们存储为对象(可能带有 lname、fname 和年份的类)。通过这种方式,您可以从用户输入中搜索 lname,将其与其相应的 fname 相匹配(作为相同的对象)。创建可以完成一次,搜索可以在一个 while 循环中完成,以实现用户同意继续搜索。

//define your class like this:
static int i; //to keep a track of number of objects
public class dummy{
string fname;
string lname;
string year;
};

while the file content exists:
read the line
dummy dobj[i++] = new dummy();//allocate memory for the object 
split the different parameters (fname, lname, year) from the read line
put these read parameters into the object 
dobj[i].fname = first;
dobj[i].lname = second;
dobj[i].year = y;

//ask your user to enter the query in a specified format
//if he enters lname, compare your input to all the object's lname, and so on
//in case of lname && fname, compare your input to the lname first and then check for the corresponding objects fname, if they match.. display
Run Code Online (Sandbox Code Playgroud)

实际上,有很多方法可以实现您想要编程的目标。您可以要求使用数组列表索引来解决它。如果您以特定格式接收用户的输入,则可以将其映射到该列表中的索引。此外,如果您想一起使用名字和姓氏,您可以使用代表来自同一列表的名字和姓氏的索引。