【问题标题】:Exit out of while loop after method iterates through file of strings and finds matched input answer方法遍历字符串文件并找到匹配的输入答案后退出while循环
【发布时间】:2018-06-10 18:55:49
【问题描述】:

我正在为一个类分配创建一个登录页面,并且在一个方法接收用户名和密码然后在多行文本文件中搜索匹配项后退出 while 循环时遇到问题。它可以找到匹配项,但会返回主方法中的输入区域并再次询问用户名。希望这是有道理的。

任何帮助将不胜感激。如您所知,我是 Java 新手,因为这段代码到处都是,而且可能有很多错误。我整晚都在想办法解决这个问题,但没有运气。谢谢!

package course.registration;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Welcome {

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the Course Registration System" + "\n");
    System.out.print("Please type Login or Register: ");
    String choice = input.nextLine();

    while (choice.equalsIgnoreCase("Login")){
        System.out.print("Please enter email address to log in: ");
        String email = input.nextLine();
        System.out.print("Please enter password: ");
        String password = input.nextLine();

        //goes to method to search and match inputs
        VerifyLogin verify = new VerifyLogin();
        verify.VerifyInfo(email, password);
        }

    if (choice.equalsIgnoreCase("Register")) {
        System.out.println("Going to registration Page...");
        }
    input.close();
    }
}

这是搜索文本文件并尝试为输入找到匹配项的方法。我觉得问题出在方法退出并返回到 main 方法中的 while 循环时。我想不出退出while循环的方法。以下是字符串在“students_logins.txt”文件中的样子:

jthomas@gmail.com,1234
kwatson@time.com,3333
legal@prog.com,d567
lavern@shirley.com,34
kwatson@gmail.com,12200

package course.registration;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class VerifyLogin {
    private String tempUsername;
    private String tempPassword;

    public void VerifyInfo(String email, String password) throws FileNotFoundException {
        boolean login = false;
        File file = new File("student_logins.txt");
        Scanner info = new Scanner(file);
        info.useDelimiter("[,\n]");

        while (info.hasNextLine()) {
            tempUsername = info.next();
            tempPassword = info.next();

            if (tempUsername.trim().equals(email.trim()) && (tempPassword.trim().equals(password.trim()))) {
                System.out.println("Email Address or Password Works!!");
                break;
            }
        }

        if (!login) {
            System.out.println("Email Address or Password is Invalid.");
        }
        info.close();
    }

}

【问题讨论】:

    标签: java while-loop java.util.scanner user-input


    【解决方案1】:

    只需将条件移动到 while 循环中,如果选定的条件是最终的,例如用户输入了有效的登录名和密码,然后使用break 退出循环。否则会继续循环:

    public class Welcome {
    
        public static void main(String... args) throws IOException {
            final LoginValidator loginValidator = new LoginValidator(Welcome.class.getResourceAsStream("student_logins.txt"));
    
            try (Scanner scan = new Scanner(System.in)) {
                System.out.println("Welcome to the Course Registration System");
    
                int choice = 0;
    
                while (choice >= 0) {
                    System.out.println();
                    System.out.println("1: LoginPlease");
                    System.out.println("2: Register");
                    System.out.print("Your choice: ");
    
                    choice = scan.nextInt();
                    scan.nextLine();
    
                    if (choice == 1) {
                        System.out.print("Please enter email address to log in: ");
                        String email = scan.nextLine();
                        System.out.print("Please enter password: ");
                        String password = scan.nextLine();
    
                        if (loginValidator.isValid(email, password)) {
                            System.out.println("Email Address or Password Works!!");
                            break;
                        } else
                            System.out.println("Email Address or Password is Invalid.");
                    } else if (choice == 2) {
                        System.out.println("Going to registration Page...");
                        break;
                    }
                }
            }
        }
    }
    

    对于验证,最好在应用程序启动时从文件中加载所有登录,然后使用它只需检查Map

    final class LoginValidator {
    
        private final Map<String, String> map = new HashMap<>();
    
        public LoginValidator(InputStream in) {
            try (Scanner scan = new Scanner(in)) {
                scan.useDelimiter("[,\n]");
    
                while (scan.hasNextLine()) {
                    map.put(scan.next(), scan.next());
                    scan.nextLine();
                }
            }
        }
    
        public boolean isValid(String email, String password) {
            return map.containsKey(email) && map.get(email).equals(password);
        }
    }
    

    【讨论】:

    • 感谢 oleg.cherednik!我能够使用代码弄清楚。我需要 LoginValidator 类底部的布尔方法。
    【解决方案2】:

    在 main 方法中,您总是停留在 while 循环中,因为您再也不会获得输入。

    在while循环之前你有:

    String choice = input.nextLine();
    

    因此,当您提供 Login 作为输入时,while 条件始终为 true,因此您将停留在此 while 循环中。

    如果你想要求用户输入正确的登录/注册直到他/她提供,你可以尝试使用我的Welcome类版本:

    public class Welcome {
    
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the Course Registration System" + "\n");
        System.out.print("Please type Login or Register: ");
        String choice = input.nextLine();
    
        while (!choice.equalsIgnoreCase("Login") && !choice.equalsIgnoreCase("Register")) {
            choice = input.nextLine();
        }
    
        if(choice.equalsIgnoreCase("Login")){
            System.out.print("Please enter email address to log in: ");
            String email = input.nextLine();
            System.out.print("Please enter password: ");
            String password = input.nextLine();
    
            //goes to method to search and match inputs
            VerifyLogin verify = new VerifyLogin();
            verify.VerifyInfo(email, password);
        }
    
        if (choice.equalsIgnoreCase("Register")) {
            System.out.println("Going to registration Page...");
        }
        input.close();
    }
    

    }

    【讨论】:

    • 感谢 Przemek 的快速响应!我正在接近,因为它正在检查登录名是有效还是无效,但如果它无效,它不会提示返回再次输入电子邮件。如果无效,则继续执行主菜单中的第二条 if 语句进行“注册”。我知道我们很接近了!
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2019-03-19
    • 2015-06-03
    • 1970-01-01
    相关资源
    最近更新 更多