2015-12-02 28 views
0

我編寫了一個程序,讓用戶不知道,除非他們知道用戶名和密碼。這只是我爲了好玩而編寫的一個程序,並沒有真正的目的。但是,在測試代碼時,我發現在嘗試從最近寫入的文件中讀取時遇到了問題。無論出於何種原因,我爲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(); 
    } 
} 
} 

我承認我的代碼是不完美的,但我認爲它應該最起碼的工作。任何幫助,將不勝感激。

+0

你有userIn.hasNextLine()兩次,而不是passIn.hasNextLine()在你的第一個條件 – Raf

回答

0

您需要重新初始化掃描儀對象以打開輸入流以更新文件內容。

System.out.print("Please enter password: "); 
String password = in.nextLine().trim(); 

以上後僅僅兩行,你需要以下條件:

userIn = new Scanner(usernameFile, "UTF-8"); 
passIn = new Scanner(passwordFile, "UTF-8"); 

您無法讀取什麼,是因爲你打開的輸入流連接使用userIn文件的用戶名和密碼的原因和passIn時,有沒有任何文件。添加上面兩行打開新的輸入流連接到更新的文件,因此,您將能夠閱讀內容。

你似乎有一點問題,下面的條件:

//if (!userIn.hasNextLine() || !userIn.hasNextLine()) { 
    if (!userIn.hasNextLine() || !passIn.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(); 

      } 
      //why again !userIn.hasNextLine(), you should check passIn 
      //if (!userIn.hasNextLine()) { 
       if (!passIn.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:"); 
     } 

我有固定的兩個條件和註釋掉的代碼位。

+0

是的,我刪除了大部分評論,但錯過了那部分。哎呀。作爲一個便箋,評論者指出了我的程序中的一個問題,但通過將passIn而不是userIn修復它使password.txt文件報告爲空。問題在於.hasNextLine()文件嗎? (我不認爲你必須像這樣重新打開輸入流,如果你改變文件的話) –

+0

@LukeKrauss你有一個if條件,你檢查if(!userIn.hasNextLine()||!passIn.hasNextLine )),目的是要求用戶通過,如果他們是空的,在這種情況下,你有兩個條件。這兩個條件都使用userIn.hasNextLine(),但只有第一個用戶應該是userIn.hasNextLine(),而另一個寫入password.txt的條件應該是用戶passIn.hasNextLine()。如果你已經更新了第一個條件,然後離開了另一個條件,很明顯,當你在第一個條件中寫入username.txt時,第二個條件變成false,所以更新它們 – Raf

+0

至於重新打開輸入流以便能夠讀取關心,這是棘手的,你可以在這個問題閱讀更多http://stackoverflow.com/questions/4149/how-do-i-use-java-to-read-from-a-file-that-is-正在積極寫作 – Raf