Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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_Java.util.scanner - Fatal编程技术网

Java 向文本文件添加名称并从用户输入中搜索名称

Java 向文本文件添加名称并从用户输入中搜索名称,java,java.util.scanner,Java,Java.util.scanner,我正在尝试使用file writer将名称添加到文本文件中,并使用scanner类搜索名称。我的问题是,当我向文本文件中添加多个名称并搜索添加到文本文件中的名称时,会显示第一个名称,但不会显示所有其他名称。 这是密码 public class Test { public static void main(String[] args) throws IOException { menu(); } public static void menu() throws IOException {

我正在尝试使用file writer将名称添加到文本文件中,并使用scanner类搜索名称。我的问题是,当我向文本文件中添加多个名称并搜索添加到文本文件中的名称时,会显示第一个名称,但不会显示所有其他名称。 这是密码

public class Test {

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

public static void menu() throws IOException {
        Scanner input = new Scanner(System.in);

        System.out.println("1. Add name");
        System.out.println("2. Done");

        int choice = input.nextInt();
        switch (choice) {
            case 1: {
                addName();
                break;
            }
            case 2: {
                verify();
                break;
            }
            default: {
                System.out.println("Invalid Selection");
                break;
            }
        }
    }

    public static void addName() {
        Scanner scan = new Scanner(System.in);

        try {

            FileWriter writer = new FileWriter("Names.txt", true);
            writer.write("\r\n");
            System.out.println("Enter Name: ");
            writer.write(scan.nextLine());

            writer.close();
            System.out.println("name added to file");

            menu();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void verify()
            throws IOException {

        Scanner textfile = new Scanner(new File("Names.txt"));
        Scanner VerifyScnr = new Scanner(System.in);
        
        System.out.print("Enter name: ");
        String Name = VerifyScnr.next();

        while (textfile.hasNext()) {
            
            String search = textfile.next();
            if (search.equalsIgnoreCase(Name)) {
                
                System.out.println("This name is in the file");
                menu();
            } else {
                System.out.println("This name is not in the file");
                verify();
            }
        }
    }
}
试试这个

public class Test {

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

    public static void menu() throws IOException {
        Scanner input = new Scanner(System.in);

        System.out.println("1. Add name");
        System.out.println("2. Search");
        System.out.println("3. Done");

        int choice = input.nextInt();
        switch (choice) {
        case 1: {
            addName();
            break;
        }
        case 2: {
            verify();
            break;
        }
        case 3: {
            // terminating execution
            System.exit(0);
        }
        default: {
            System.out.println("Invalid Selection");
            break;
        }
        }
    }

    public static void addName() {
        Scanner scan = new Scanner(System.in);

        try {

            FileWriter writer = new FileWriter("Names.txt", true);
            writer.write("\r\n");
            System.out.println("Enter Name: ");
            writer.write(scan.nextLine());

            writer.close();
            System.out.println("name added to file");

            menu();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void verify() throws IOException {

        Scanner textfile = new Scanner(new File("Names.txt"));
        Scanner VerifyScnr = new Scanner(System.in);
        boolean isFound = false;
        System.out.print("Enter name: ");
        String Name = VerifyScnr.next();

        while (textfile.hasNext()) {
            String search = textfile.next();
            if (search.equalsIgnoreCase(Name)) {
                System.out.println(Name + " is in the file");
                isFound = true;
                break;
            }
        }
        if (!isFound) {
            System.out.println(Name + " is not in the file");
        }
        menu();
    }
}

无关:始终遵循java命名约定。您所有的变量名都应该是大写的。只需删除
verify()else
条件中选择code>。如果调用
verify()
,它将始终从一开始就重新启动进程,除第一个元素外,不会获得任何其他元素。您还应移动
System.out.println(“此名称不在文件中”)
while
语句之外,因为只有在读取文件的所有值后找不到该名称时,才能知道该名称是否不在文件中。