java - 字符串数组名称搜索

标签 java file java.util.scanner

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

需要读入该文件,用户可以输入总统姓名进行搜索,并显示该总统。

如果用户按名字或姓氏而不是同时搜索,我会显示总统。

例如,外部文件包含:

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

我需要用户能够输入名字、姓氏或同时输入名字和姓氏并获得所需的结果(日期与大部分内容无关)。

尝试了很多不同的方法,但没有达到在用户按名字和姓氏搜索时显示总统的程度。

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

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

}

最佳答案

我可能还有另一种实现方式。读取文件输入并将它们存储为对象(可能带有 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

实际上,您可以通过多种方式实现您想要的编程。您可以要求使用数组列表索引来解决它。如果您以特定格式接收用户的输入,则可以将其映射到该列表中的索引。此外,如果你想同时使用名字和姓氏,你可以使用这些表示名字和姓氏的索引来自同一个列表。

关于java - 字符串数组名称搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703789/

相关文章:

file - WAV文件数据恢复

java - 通过扫描仪更新二维阵列

java - OpenCV CascadeClassifier 错误

java - 如何从子类调用重写的父类方法?

java - Oracle Weblogic 10.3.6 上的 Dozer 5.3.2

java - Tomcat 何时/如何选择 ServletContext 接口(interface)的具体类?

javascript - 无法生成使用js或php下载的文件

javascript - 在 NodeJS 中从 URL 获取图像并通过 POST 上传到另一个

java - Java 中的 Getter 类接受用户输入

java - 尝试输出数学方程的解总是结果为 0