首页 > 解决方案 > 扫描仪不会继续读取多行文件进行登录功能

问题描述

我有一个登录按钮,它从 text.file 读取两行文本,第一行是用户名,第二行是密码。文本文件中有多个用户名和密码,但该代码仅适用于第一对。

btLogin.setOnAction(e -> {
   boolean grantAccess = false;
        String userName = tfUsername.getText();
        String password = tfPassword.getText();
    File f = new File("users.txt");
    try { 
    Scanner read = new Scanner(f);
    int lineNo=0;
    while(read.hasNextLine()) {
        lineNo++;
    String loginInfo = read.nextLine();

    if ((lineNo == 1)&& (loginInfo.equals(userName))) {
        continue;
    }
    else if ((lineNo == 2) && (loginInfo.equals(password))) {
        grantAccess=true;
        break;
    }
    else {
        break;
        }
    }

标签: buttonjavafxwhile-loop

解决方案


推荐阅读