Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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 While循环中的Try子句在1个循环后未执行_Java_While Loop_Try Catch - Fatal编程技术网

Java While循环中的Try子句在1个循环后未执行

Java While循环中的Try子句在1个循环后未执行,java,while-loop,try-catch,Java,While Loop,Try Catch,我的java类。它有所有必要的软件包。我正在使用命令行界面 public static boolean checkLibIDPass(){ boolean continueLogin = true; boolean check = false; boolean retry = false; while (continueLogin){ Scanner Sc = new Scanner(System.in); String _id,

我的java类。它有所有必要的软件包。我正在使用命令行界面

public static boolean checkLibIDPass(){
    boolean continueLogin = true;
    boolean check = false;
    boolean retry = false;

    while (continueLogin){
        Scanner Sc = new Scanner(System.in);
        String _id,_pass;
        int goBack;

        System.out.println("Input Librarian ID: ");
        _id = Sc.next();
        System.out.println("Input Librarian Password: ");
        _pass = Sc.next();

        try {
            BufferedReader br = new BufferedReader(new FileReader("LibrarianFile.txt"));
            String line = null;

            while ((line = br.readLine()) != null && retry == false) {
                String[] values = line.split("\t",-1);

                for (int i=0; i < values.length; i++) {
                    if((_id.equals(values[0])) && (_pass.equals(values[1]))){
                        check = true;
                        retry = true;
                        System.out.println("Login Successful!");
                        break;
                    }
                    else if(i == values.length -1){
                        System.out.println("Librarian ID or Password is invalid");
                        System.out.println("Retry Login?" + "\n" + "1 : Yes"+ "\n" + "2 : Choose another user");
                        goBack = Sc.nextInt();
                        if (goBack == 1){
                            retry = true;
                            continueLogin = true;
                            break;
                        }
                        else if (goBack == 2){
                            retry = true;
                            continueLogin = false;
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }
    return check;
}

我的目标是再次访问try子句,如果我想重新输入另一个ID和密码。但是,它总是在没有try子句的情况下循环

评论变成了回答。不确定这是否真的解决了您的问题,我没有运行代码。
可能还有其他问题。但是,在任何情况下,下面列出的问题都应该得到解决


您的代码大大简化了:

public static boolean checkLibIDPass(){
    boolean continueLogin = true;
    boolean check = false;

    while (continueLogin) {
        Scanner Sc = new Scanner(System.in);
        String _id,_pass;

        System.out.println("Input Librarian ID: ");
        _id = Sc.next();
        System.out.println("Input Librarian Password: ");
        _pass = Sc.next();

        try {
            BufferedReader br = new BufferedReader(new FileReader("LibrarianFile.txt"));
            /* Do lots of stuff */
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return check;
}
请注意,您在循环中打开了一个
BufferedReader
。如果您再次进入循环,您将尝试再次打开它-在同一个文件上!这不是一个好主意,除非你之前关闭了阅读器。您应该执行以下操作之一:

  • 在循环结束时关闭读卡器
  • 在循环外部打开读卡器,重新使用读卡器
  • 我推荐第二种方法,因为它应该表现得更好。现在我们已经完成了,您还可以将
    扫描仪
    从循环中取出:

    public static boolean checkLibIDPass(){
        boolean continueLogin = true;
        boolean check = false;
    
        Scanner sc = new Scanner(System.in); // create once
        String _id,_pass;
    
        BufferedReader br;
    
        try {
            BufferedReader br = new BufferedReader(new FileReader("LibrarianFile.txt")); // create once
            br.mark(); // remember this position (beginning)
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    
        while (continueLogin) {
            System.out.println("Input Librarian ID: ");
            _id = sc.next();
            System.out.println("Input Librarian Password: ");
            _pass = sc.next();
    
            br.reset(); // go back to the beginning
            /* Do lots of stuff */
        }
        br.close(); // release the file
        sc.close(); // also best to close the scanner
        return check;
    }
    

    <强>一个附加注释:考虑进一步拆分你的方法,所以你最终得到了简短易读的方法,每个方法都有一个非常有限的、明确定义的责任。这也会使在代码中发现bug变得更容易。

    极大地提高了代码的可读性,但要回答问题标题中的隐含问题:(为什么“While循环中的Try子句在1次循环后不执行”


    实际上,try子句会再次执行,但这并不明显,因为内部while循环不再执行。这是因为您在每种情况下都将
    retry
    设置为true,而内部while循环的条件是
    retry==false

    您是否关闭过
    BufferedReader
    ?我不这么认为。要么在循环中关闭它,要么将该行从循环中取出,以便打开文件一次,然后在循环中重新使用读取器。然后在循环结束后关闭它。看看这是否有帮助。我想你的意思是,如果登录无效,你想重新读取文件。这不会发生在代码中,因为while循环在try中,而文件读取在while之外。您可以使用一个“已验证”布尔值,并在一段时间内包装读取的文件(bool=false),然后在当前循环内将bool设置为true(如果凭据有效)。难怪我应该在外部while循环中设置retry==false,以便每次都重置。我发现很难拆分此代码,因为它主要在循环中,并且非常特定于手头的任务。顺便说一句,BufferedReader br必须在外部while循环中,因为我在其中使用了br.readLine()。我在搜索错误时忘了回写br.close,谢谢提醒。Raika,在程序开始时阅读文本文件,将其保存到
    HashMap
    或其他适当的集合中。这将大大简化id和密码的查找,并删除
    checkLibIDPass()
    中的循环。然后,将方法拆分,例如一个方法获取用户输入,另一个方法检查输入是否有效?然后可以将业务逻辑简化为几行,根据需要随时调用这两个方法
    public static boolean checkLibIDPass(){
        boolean continueLogin = true;
        boolean check = false;
    
        Scanner sc = new Scanner(System.in); // create once
        String _id,_pass;
    
        BufferedReader br;
    
        try {
            BufferedReader br = new BufferedReader(new FileReader("LibrarianFile.txt")); // create once
            br.mark(); // remember this position (beginning)
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    
        while (continueLogin) {
            System.out.println("Input Librarian ID: ");
            _id = sc.next();
            System.out.println("Input Librarian Password: ");
            _pass = sc.next();
    
            br.reset(); // go back to the beginning
            /* Do lots of stuff */
        }
        br.close(); // release the file
        sc.close(); // also best to close the scanner
        return check;
    }