Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串数组名称搜索_Java_File_Java.util.scanner - Fatal编程技术网

Java 字符串数组名称搜索

Java 字符串数组名称搜索,java,file,java.util.scanner,Java,File,Java.util.scanner,所以我有一个文件,里面有所有总统的名字,中间的首字母(如果有的话)和姓氏 文件需要读入,用户可以输入总统的姓名进行搜索,并显示总统 如果用户按名字或姓氏搜索,而不是按两个名字搜索,我会让它显示总统 例如,外部文件包含: George,Washington,(1789-1797) Franklin,D.,Roosevelt,(1933-1945) ... and so on with all the presidents 我需要用户能够输入名字、姓氏,或者同时输入名字和姓氏,并获得所需的结果(日

所以我有一个文件,里面有所有总统的名字,中间的首字母(如果有的话)和姓氏

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

如果用户按名字或姓氏搜索,而不是按两个名字搜索,我会让它显示总统

例如,外部文件包含:

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

}
公共类名称搜索{
公共静态void main(字符串[]args)引发IOException{
试一试{
//从总统档案中读取
Scanner presidentsFile=新扫描仪(新文件(“Presidents.txt”);
//用户输入扫描仪
扫描仪键盘=新扫描仪(System.in);
//创建文件中每行的数组列表
ArrayList presidentsArrayList=新建ArrayList();
//提示用户输入字符串,查看是否与总统姓名匹配
System.out.println(“输入搜索字符串以查找匹配的总统:”);
//存储用户输入
字符串userInput=keyboard.nextLine();
//将总统文件信息添加到阵列列表行总统文件
while(presidentsFile.hasNextLine()){
add(presidentsFile.nextLine());
}//在循环结束时结束
字符串presidentNamesArray[]=presidentsArrayList.toArray(新字符串[presidentsArrayList.size());
字符串结果=searchArray(presidentNamesArray,userInput);
//System.out.println(“\n名称中包含\”“+userInput+”\”的总统是:”;
}捕获(FileNotFoundException ex){
//打印输出错误(如果有)到屏幕
System.out.println(例如toString());
}//末端挡块
}//末端总管
//方法来搜索数组中的特定值
公共静态字符串搜索数组(字符串数组[],字符串值){
for(int i=0;i
我还可以用另一种方法实现这一点。读取文件输入并将其存储为对象(类可能包含lname、fname和year)。通过这种方式,您可以从用户输入中搜索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

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

按名字和姓氏搜索时可能出现问题的原因是您必须精确匹配输入(当然忽略大小写)。我的意思是,如果您使用
George Washington
作为输入,您的程序将找不到与
George,Washington(1789-1797)
行匹配的代码。这是因为您的程序将
George Washington
视为一个字符串。注意:输入缺少逗号,因此它不会被视为华盛顿乔治(1789-1797)的子字符串。如果使用
George,Washington
作为输入字符串,则程序将打印George Washington行。您的程序只是搜索输入字符串是否是文件中任何行的子字符串。它不会专门搜索名字或姓氏。如果您使用中的
作为输入字符串,那么您将获得gton中的乔治·华盛顿D·罗斯福中的弗兰克尔的匹配项。

您可以做的是将输入数据拆分并搜索每个术语。您可以接受与提供的所有条款匹配的行,也可以接受至少一个条款

public static String searchArray(String array[], String value) {
    // Uses both blank spaces and commas as delimiters
    String[] terms = value.toLowerCase().Split("[ ,]"); 

    for (int i = 0; i < array.length; i++) {
        String line = array[i].toLowerCase();
        boolean printIfAllMatch = true;
        boolean printIfAtLeastOneMatches = false;
        for(int j = 0 ; j < terms.length; j++) {
            // Check that all terms are contained in the line
            printIfAllMatch &= line.Contains(terms[j]); 
            // Check that at least one term is in the line
            printIfAtLeastOneMatches |= line.Contains(terms[j]); 
        }

        String splitter[] = array[i].split(" ,");
        if (printIfAllMatch) {
            System.out.println(Arrays.toString(splitter));
        }
        if(printIfAtLeastOneMatches) {
            System.out.println(Arrays.toString(splitter));
        }

    }
    //I'm not sure why you're returning the original array as a string
    //I would think it would make more sense to return an Array of filtered data.
    return Arrays.toString(array);
}
公共静态字符串搜索数组(字符串数组[],字符串值){
//使用空格和逗号作为分隔符
String[]terms=value.toLowerCase().Split(“[,]”);
for(int i=0;i
这不考虑名称排序。如果这就是您想要的,那么我建议创建一个类,并将文件中的每一行解析为一个新对象,并尝试将提供的第一个术语与第一个名称和第二个名称相匹配