2012-07-30 105 views
0

對不起,以下冗長的代碼,我的代碼能夠執行並在用戶輸入選項'5'時正常運行。我已經獲得了用戶輸入的所有值,並且沒有錯誤,但沒有寫入我的文件。我試圖寫入一個已經包含信息的文件

我目前的文件中有數據:

Anthony Ducan;anthony;a123;55 Peter Street;3321444;VISA;3213504011223 
Barry Blake;barry;a999;456 George Street;23239876;VISA;435677779876 
Claire Rerg;clare;c678;925 Edward Lane;67893344;MASTERCARD;223344556677 

我試圖存儲精確的方式爲我的文本文件相同。 在void writeLinesToFile()方法中,我的編碼有問題嗎?或者它是void newCust()方法嗎?

仍然困惑,我沒有錯誤,但它沒有寫入我的文件。

public MainPage1(){ //start of MainPage1() 

     System.out.println("====================================================="); 
     System.out.println("Kreg Hotel Booking System - Main Page"); 
     System.out.println("====================================================="); 
     System.out.println("[1] General Information"); 
     System.out.println("[2] Make Booking"); 
     System.out.println("[3] Active Booking Summary"); 
     System.out.println("[4] Existing Customer"); 
     System.out.println("[5] New Customer"); 
     System.out.println("[6] Check Booking Status"); 
     System.out.println("[7] Promotions"); 
     System.out.println("[8] Exit\n"); 
     System.out.println("Note: You have to select option 2 & 3 before option 4 & 5\n"); 
     System.out.println("Please enter your selection: ");   

try{choice = input.nextInt();}//to try to see if user input integer 

     catch (java.util.InputMismatchException e){//to catch if user didnt input integer 
      System.out.println("Invalid Input"); 
      return; 
     } 


     while(true){ //start of do-while loop 

      if(choice==4||choice==5) 
      { 
       if(roomInfo.isEmpty()==true || addOns.isEmpty()== true){ 
        System.out.println("Please enter option 2 or 3 before option 4 or 5"); 
        choice=input.nextInt(); 
       } 
       else if(choice == 4){existingCustomers();} 
       else if(choice ==5){newCust();} 
       else new MainPage1();    
      } 

     switch(choice){ 

       case 1:break; 
       case 2:break; 
       case 3:break; 
       case 4:break; 
       case 5:newCust(); 
         break; 
       case 6:break; 
       case 7:break; 
       case 8:System.out.println("Thank you. See you again soon");//exit the menu 
         System.exit(0);break; 
       default:System.out.println("Please enter number between 1 to 8");//prompts the user if they didnt enter between 1 to 8 
         choice = input.nextInt(); 
         break; 
      } 

     }//while(choice!=1 || choice !=2 || choice!=3 ||choice!=4); // end of do-while loop 



    }//end of MainPage1() 


public static void main(String[] args){//start of main page 


    new MainPage1();//to call the constructor 


}//end of mainpage 



public void writeLinesToFile(String filename,String[] linesToWrite,boolean appendToFile){ 

    PrintWriter pw = null; 

    try { 

     if (appendToFile) { 

     //If the file already exists, start writing at the end of it. 
     pw = new PrintWriter(new FileWriter(filename, true)); 

     } 
     else { 

     pw = new PrintWriter(new FileWriter(filename)); 
     //this is equal to: 
     //pw = new PrintWriter(new FileWriter(filename, false)); 

     } 

     for (int i = 0; i < linesToWrite.length; i++) { 

     pw.println(linesToWrite[i]+";"); 

     } 
     pw.flush(); 

    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 

     //Close the PrintWriter 
     if (pw != null) 
     pw.close(); 

    } 

    } 

    public void newCust(){ 

    System.out.println("Welcome to Kreg Hotel Booking System"); 
    System.out.println("=============================================="); 
    System.out.println("Please enter your name: "); 
    n = input.nextLine(); 
    n = input.nextLine(); 
    System.out.println("Please enter your login username: "); 
    un = input.nextLine(); 
    System.out.println("Please enter your login password: "); 
    pw = input.nextLine(); 
    System.out.println("Please enter your address: "); 
    a = input.nextLine(); 
    System.out.println("Please enter your contact: "); 
    con = input.nextLine(); 
    System.out.println("Please enter your credit card type: "); 
    ct = input.nextLine(); 
    System.out.println("Please enter your credit card number: "); 
    cn = input.nextLine(); 
    MainPage1 util = new MainPage1(); 
    util.writeLinesToFile("customerinfo.txt",new String[]{n,un,pw,a,con,ct,cn},true); 
    new MainPage1(); 

    } 
}//end of class 
+0

ok我發現我的問題,它在我的虛空newCust()方法。而不是做MainPage1 util = new MainPage1(); util.writeLinesToFile(「customerinfo.txt」,new String [] {n,un,pw,a,con,ct,cn},true);我juz直接使用writeLinesToFile() – 2012-07-30 19:45:10

+0

我希望你發佈的數據不是真實的。如果我是你的老闆,我會很生氣...... – fneron 2012-07-30 19:48:33

+0

@fneron它的juz我從學校的任務,我發佈的數據有嚴重的問題? – 2012-07-30 19:53:36

回答

0

看一看FileBufferedWriterFileWriter文檔。你應該可以用這兩個班來完成這個任務。這段代碼應該讓你開始。

String content = "Add this everytime"; 

File file =new File("example.txt"); 

//if file does not exists, then create it 
if(!file.exists()){ 
    file.createNewFile(); 
} 

//true = append file 
FileWriter fileWritter = new FileWriter(file.getName(),true); 
BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 
bufferWritter.write(content); 
bufferWritter.close(); 

System.out.println("Done"); 
+0

thx會看看它,btw file.createNewFile(),你在哪裏創建新文件? – 2012-07-30 20:14:20

+0

http://docs.oracle.com/javase/7/docs/api/java/io/File.html。查看文檔更多,你會看到你需要的所有信息都在那裏。回答你的問題,這取決於實例。所以在這裏,它應該是你程序的根源。但我可以說新的文件(root/folder1/folder2/example.txt)。如果該文件不是我指定的地方,它可以簡單地創建它。理解?不要忘記投票。 – fneron 2012-07-30 20:17:27

+0

您可以跳過檢查文件是否存在,只需將文件名作爲字符串傳遞給FileWriter構造函數。 – 2012-07-30 20:22:40

相關問題