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

时间:2017-04-11 09:56:28

标签: 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,_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;
}

我的输出:

Welcome to Knowledgica's Library Management System !
Which type of user are you?
1 : Guest
2 : Librarian
3 : Admin
4 : Exit
2
Input Librarian ID: 
aasdasd
Input Librarian Password: 
asdasdasd
Librarian ID or Password is invalid
Retry Login?
1 : Yes
2 : Choose another user
1
Input Librarian ID: 
asdasd
Input Librarian Password: 
asdasd
Input Librarian ID: 
sdasd
Input Librarian Password: 
asdasdas
Input Librarian ID: 

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

2 个答案:

答案 0 :(得分:0)

评论转过来了。不确定这是否真的解决了你的问题,我没有运行代码 可能还有其他问题。但是,在任何情况下都应该修复下面列出的那些。

您的代码,大大简化:

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。如果您再次进入循环,则尝试再次打开它 - 在同一个文件上!除非你之前关闭读者,否则这不是一个好主意。您应该执行以下操作之一:

  1. 在循环结束时关闭阅读器
  2. 通过在循环外打开
  3. 重新使用阅读器

    我推荐第二种方法,因为它应该表现得更好。现在我们就可以了,你也可以将Scanner带出循环:

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

    另外一点注意事项:考虑进一步拆分您的方法,这样您最终会得到简短易读的方法,每个方法都有一个非常有限的,定义明确的方法。它应该可以更容易地在代码中找到错误。

答案 1 :(得分:0)

domsson's answer极大地提高了代码的可读性,但回答了问题标题中的隐含问题:(为什么)“循环中的Try子句在1循环后没有执行”?

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