Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 Else语句在处理所有文本文件之前执行_Java - Fatal编程技术网

Java Else语句在处理所有文本文件之前执行

Java Else语句在处理所有文本文件之前执行,java,Java,我是一个新的程序员 我正在编写一个登录系统,可以通过文件处理验证用户名和密码 我遇到了这个问题,我的登录系统可以验证my credentials.txt中的第一对凭据,但如果前两个凭据不匹配,则在执行“else”语句时,它没有机会验证第二对凭据 一切帮助都将不胜感激 public void verifyLogin(String username, String password) throws FileNotFoundException { try { File rea

我是一个新的程序员

我正在编写一个登录系统,可以通过文件处理验证用户名和密码

我遇到了这个问题,我的登录系统可以验证my credentials.txt中的第一对凭据,但如果前两个凭据不匹配,则在执行“else”语句时,它没有机会验证第二对凭据

一切帮助都将不胜感激

public void verifyLogin(String username, String password) throws FileNotFoundException {

    try {
        File readCredentials = new File("credentials.txt");

        Scanner readFile = new Scanner(readCredentials);

        boolean matchFound = false;

        while (readFile.hasNextLine() && matchFound == false) {

            uIUsername = readFile.nextLine();
            uIPassword = readFile.nextLine();

            if (uIUsername.equals(username) && uIPassword.equals(password)) {

                System.out.println("You are logged in");

                readFile.close();

                matchFound = true;
            }
            else {
                System.out.println("Error");

                readFile.close();

                matchFound = true;
            }
        }
    }
    catch (Exception e) {
        System.out.println("Exception");
    }
}

循环完成后,您需要打印错误消息。按如下方式操作:

public void verifyLogin(String username, String password) {
    Scanner readFile;
    try {
        File readCredentials = new File("credentials.txt");
        readFile = new Scanner(readCredentials);
        boolean matchFound = false;
        while (readFile.hasNextLine() && matchFound == false) {
            uIUsername = readFile.nextLine();
            uIPassword = readFile.nextLine();
            if (uIUsername.equals(username) && uIPassword.equals(password)) {
                System.out.println("You are logged in");
                matchFound = true;
            }
        }
        if (!matchFound) {
            System.out.println("Error");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    readFile.close();
}
请注意,我也只放置了一次
readFile.close()
,而不是在
if
else
部分中重复它


另外,请注意,我使用了
e.printStackTrace()
,这将确保在出现任何异常时打印完整的stacktrace。

不要在while循环中关闭扫描仪,如果要继续,请在外部关闭它。还有,就说吧!matchFound而不是matchFound==false这是因为
else
语句。检查第一个凭据,如果它们不匹配,则运行
else
。如果文件包含多个用户,则不能使用
else
。而是将代码移到循环之后。也就是说,我希望这只是一个玩具项目。在现实生活中,永远不要存储密码,只存储(盐渍)密码散列,这样攻击者就无法窃取您的密码数据库。感谢您的帮助!不用担心,这只是一个玩具项目!非常感谢你的回答,它现在完全起作用了。还感谢您提供的其他提示:-)