エラーが発生します
ローカル変数名とパスワードが初期化されていない可能性があります。
if ステートメントの場合。これらのエラーは、括弧内の 2 番目の文字列を引用符で囲んだものに変更したり、変数を 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();
}
}