我編寫了一個程序,讓用戶不知道,除非他們知道用戶名和密碼。這只是我爲了好玩而編寫的一個程序,並沒有真正的目的。但是,在測試代碼時,我發現在嘗試從最近寫入的文件中讀取時遇到了問題。無論出於何種原因,我爲username.txt文件創建的掃描程序對象userOut並未檢測到它們是文件中的任何內容(即.hasNextLine()返回false),但如果在程序中寫入則只有。如果我寫一些文本到文件,然後運行該程序,它工作得很好。請注意,password.txt文件完全正常。這是爲什麼發生?一個掃描儀對象沒有檢測到寫入程序中的任何文件
這裏是我的代碼:
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class Challenge5 {
public static void main(String[] args) {
try {
File usernameFile = new File("username.txt");
File passwordFile = new File("password.txt");
PrintWriter userOut = new PrintWriter(new FileOutputStream(usernameFile, true));
PrintWriter passOut = new PrintWriter(new FileOutputStream(passwordFile, true));
Scanner in = new Scanner(System.in);
Scanner userIn = new Scanner(usernameFile, "UTF-8");
Scanner passIn = new Scanner(passwordFile, "UTF-8");
System.out.println("Welcome to Luke's secure program.");
if (!userIn.hasNextLine() || !userIn.hasNextLine()) {
System.out.println("New users must set up their account: ");
if (!userIn.hasNextLine()) {
System.out.print("Enter a username: ");
String username = in.nextLine().trim();
userOut.print(username);
userOut.flush();
}
if (!userIn.hasNextLine()) {
System.out.print("Enter a password: ");
String password = in.nextLine().trim();
passOut.print(password);
passOut.flush();
}
System.out.println("Your account has been created. You may now log in:");
}
System.out.print("Please enter username: ");
String username = in.nextLine().trim();
System.out.print("Please enter password: ");
String password = in.nextLine().trim();
String fileUsername = "";
while (userIn.hasNextLine()) {
fileUsername = userIn.nextLine();
}
System.out.println(fileUsername);
String filePassword = "";
while (passIn.hasNextLine()) {
filePassword = passIn.nextLine();
}
System.out.println(filePassword);
if (username.equals(fileUsername) && password.equals(filePassword)) {
System.out.println("Welcome!");
while (true) {
System.out.println("What would you like to do? ");
System.out.println("1. Change username\n2. Change password\n3. Exit");
System.out.print("> ");
int choice;
while (!in.hasNextInt()) {
System.out.println("What would you like to do? ");
System.out.println("1. Change username\n2. Change password\n3. Exit");
System.out.print("> ");
in.next();
}
choice = in.nextInt();
in.nextLine();
if (choice == 1) {
userOut = new PrintWriter(new FileOutputStream(usernameFile, false));
System.out.print("Enter new username: ");
String newUsername = in.nextLine().trim();
userOut.print(newUsername);
userOut.flush();
} else if (choice == 2) {
passOut = new PrintWriter(new FileOutputStream(passwordFile, false));;
System.out.print("Enter new password: ");
String newPassword = in.nextLine().trim();
passOut.print(newPassword);
passOut.flush();
} else if (choice == 3) {
System.out.println("Goodbye!");
break;
} else {
System.out.println("Please try again.");
}
}
} else {
System.out.println("INVALID USERNAME OR PASSWORD, EXITING PROGRAM...");
}
userOut.close();
passOut.close();
in.close();
userIn.close();
passIn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我承認我的代碼是不完美的,但我認爲它應該最起碼的工作。任何幫助,將不勝感激。
你有userIn.hasNextLine()兩次,而不是passIn.hasNextLine()在你的第一個條件 – Raf