Java登录代码从文件中读取多行代码无效
我得到下面的代码来做一个简单的密码(哈希)检查函数。但我遇到了一个问题,代码似乎只适用于文件中的单行数据,检查适用于第1行,但不适用于第2行,我不确定出了什么问题。数据如下所示 结果应该是hashedP匹配第1行或第2行。但它最终只与第1行匹配Java登录代码从文件中读取多行代码无效,java,Java,我得到下面的代码来做一个简单的密码(哈希)检查函数。但我遇到了一个问题,代码似乎只适用于文件中的单行数据,检查适用于第1行,但不适用于第2行,我不确定出了什么问题。数据如下所示 结果应该是hashedP匹配第1行或第2行。但它最终只与第1行匹配 260670134225f2a24b59121739fec73584b0ddb6b49c39e31bd1df5483ac144d //Line1 cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0
260670134225f2a24b59121739fec73584b0ddb6b49c39e31bd1df5483ac144d //Line1
cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0e87dfc9a90 //Line2
代码:
让我们分析一下这一部分:
while (scanner.hasNextLine()) {
String fileline = scanner.nextLine();
if (!(fileline.equals(hashedP))) {
System.out.println("Login Failed!");
LoginMenu();
}
else {
System.out.println("Login Successful.\n");
AdminMenu();
}
}
while
循环3.2. 如果不匹配,我们将提示用户再次登录
您应该重构循环以更加努力:
boolean failed = true;
while (scanner.hasNextLine())
{
String fileline = scanner.nextLine();
if (fileline.equals(hashedP))
{
failed = false;
System.out.println("Login Successful.\n");
AdminMenu();
}
}
if(failed)
{
System.out.println("Login Failed!");
LoginMenu();
}
与此无关,如果以任何方式都可以避免,则递归调用函数是一个好主意
在这种情况下,例如,admin.dat
和一个新的stdin
扫描器将在输入错误密码时打开多次,这是一个设计拙劣的密码。我建议使用
while(true)
循环来读取密码,并在读取密码之前执行所有其他操作:
public static void LoginMenu()
{
ArrayList<String> hashes = new ArrayList<String>();
File file = new File("admin.dat");
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
hashes.add(scanner.nextLine());
}
scanner.close();
}
catch (FileNotFoundException exc)
{
exc.printStackTrace();
return;
}
Scanner UserPass = new Scanner(System.in);
while (true)
{
System.out.println("Please Enter Your Password: ");
String UserP = UserPass.nextLine();
String hashedP = Utility.getHash(UserP);
if (hashes.contains(hashedP))
{
System.out.println("Login Successful.\n");
AdminMenu();
break;
}
System.out.println("Login Failed!");
}
}
publicstaticvoidloginmenu()
{
ArrayList哈希=新的ArrayList();
File File=新文件(“admin.dat”);
尝试
{
扫描仪=新扫描仪(文件);
while(scanner.hasNextLine())
{
add(scanner.nextLine());
}
scanner.close();
}
捕获(FileNotFoundException exc)
{
exc.printStackTrace();
返回;
}
Scanner UserPass=新扫描仪(System.in);
while(true)
{
System.out.println(“请输入您的密码:”);
字符串UserP=UserPass.nextLine();
字符串hashedP=Utility.getHash(UserP);
if(hashes.contains(hashedP))
{
System.out.println(“登录成功。\n”);
AdminMenu();
打破
}
System.out.println(“登录失败!”);
}
}
您的预期输出到底是什么?实际输出是什么?预期输出应该是hashedP,与文件中的任何一行匹配,但最终只与advice的Line1Thanks匹配(我看一下=)
public static void LoginMenu()
{
ArrayList<String> hashes = new ArrayList<String>();
File file = new File("admin.dat");
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
hashes.add(scanner.nextLine());
}
scanner.close();
}
catch (FileNotFoundException exc)
{
exc.printStackTrace();
return;
}
Scanner UserPass = new Scanner(System.in);
while (true)
{
System.out.println("Please Enter Your Password: ");
String UserP = UserPass.nextLine();
String hashedP = Utility.getHash(UserP);
if (hashes.contains(hashedP))
{
System.out.println("Login Successful.\n");
AdminMenu();
break;
}
System.out.println("Login Failed!");
}
}