我正在为类分配创建一个登录页面,并且在方法接收用户名和密码然后在多行文本文件中搜索匹配后,在退出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();
}
}
答案 0 :(得分:0)
在main方法中,你总是停留在while循环中,因为你再也没有获得输入了。
在while循环之前你有:
String choice = input.nextLine();
因此,当您提供登录作为输入,而条件始终为 true 时,您将继续使用此循环。
如果您想要用户提供正确的输入登录/注册,直到他/她提供,您可以尝试使用我的欢迎类的版本:
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();
}
}
答案 1 :(得分:0)
只需移动条件insede 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);
}
}