我收到错误
局部变量名称和密码可能尚未初始化,
对于 if 语句。如果我将括号中的第二个字符串更改为引号中的内容,或者如果我将变量设置为 0,这些错误就会消失,但是我还需要将它们更改为int
并且我需要它们为String
.
我正在尝试将文本文件中的用户名和密码与新输入的用户名和密码进行比较。该程序也应该在 3 次错误尝试后退出,所以我可能也把它System.out.println("Goodbye!");
放在了错误的地方。
public static void main(String[] args) {
int numberOfAttempts = 1;
String fileName = "logins.txt";
Scanner inputStream = null;
String name, password, line, secondname;
String secondpassword;
do
{
System.out.println("Please enter your username: ");
Scanner keyboard = new Scanner(System.in);
secondname = keyboard.nextLine();
System.out.println("Please enter your password: ");
secondpassword = keyboard.nextLine();
try
{
inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " +
fileName);
System.exit(0);
}
while (inputStream.hasNextLine())
{
line = inputStream.nextLine();
}
if ((name.equalsIgnoreCase(secondname))&&
(password.equalsIgnoreCase(secondpassword)))
{System.out.println("Welcome!");
numberOfAttempts = 4;
}
else
System.out.println("Invalid. Please try again.");
numberOfAttempts++;
}
while ( numberOfAttempts <4);
System.out.println("Goodbye!");
inputStream.close();
}
}