2014-10-29 80 views
0

有一個問題在這裏,我需要這個循環來打印新的代碼行到一個文件,直到它做什麼是打印1行然後第二次失敗,java while while try catch,can not print to a new line

永遠無法得到它打印到另一條線,下面是代碼

public class study { 
    public static void main(String[] args) throws IOException{ 
     BufferedWriter post = null; 
     File file = new File("text.txt"); 

      if(!file.exists()){ 
       file.createNewFile(); 
      } 
      boolean promptUser = true; 
      FileWriter fileWriter = new FileWriter(file); 
      post = new BufferedWriter(fileWriter); 
      try { 
       while(promptUser){ 
        System.out.println("enter age "); //get age 
        Scanner getage = new Scanner(System.in); 
        int age= getage.nextInt(); 

        if(age <20 || age>50){ //age range 
         System.out.println("age must be between 20 and 50"); 
         System.exit(0); 
        } 
        System.out.println("enter name ");  //get name 
        Scanner getname = new Scanner(System.in);  
        String name= getname.nextLine(); 

        System.out.println("enter email "); //get email 
        Scanner getarea = new Scanner(System.in);  
        String email= getarea.nextLine(); 

        post.write(age + "\t"); <===== fails here on second run 
        post.write(name + "\t");      
        post.write(email + "\t"); 
        post.newLine(); 
        post.close(); 

        System.out.println("enter quit to quit or any key to continue"); 
        Scanner options = new Scanner(System.in); 
        String option = options.nextLine();  

        if(option.equalsIgnoreCase("quit")){    
          System.out.println("goodbye!"); 
          System.exit(0); 
        } 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
+1

爲什麼不使用bufferedWriter.newLine()的;每個條目之後,而在你的情況寫入文件,即post.newLine(); – 2014-10-29 05:12:55

+0

我在關閉之前做了 – user3766275 2014-10-29 05:15:51

+0

爲什麼不在每個post.write()後都在嘗試 – 2014-10-29 05:17:19

回答

1
  post.write(age + "\t"); 
      post.newLine(); 
      post.write(name + "\t");      
      post.newLine(); 
      post.write(email + "\t"); 
      post.newLine(); 

//刪除post.close();從這裏 現在它可能會解決您的問題

+0

我的問題是,我需要程序循環,並採取數據,直到我說退出,文件只顯示1行數據,這是最後一個輸入的數據,我需要它繼續每一組數據的下一行 年齡,姓名,電子郵件(THEN NEXT LINE) 年齡,姓名,電子郵件 – user3766275 2014-10-29 05:31:42

+0

好吧,那麼你必須將輸入值存儲到一組數組中,然後寫入或調用任何在用戶輸入後準確寫入這些數據的函數。QUIT – 2014-10-29 05:34:33

+0

對陣列不好,你能舉個例子嗎? – user3766275 2014-10-29 05:37:13

0

替換post.close();post.flush();,你應該沒問題。

輸入退出條件時關閉流。

+0

同樣的東西,文件中有1行數據 – user3766275 2014-10-29 05:45:40

+0

@ user3766275我照原樣複製,只改變了上面提到的那一行,它就起作用了。 – Tirath 2014-10-29 06:08:59

0

固定它GUYS

我需要的FileWriter的線遷出TRY

import java.io.*; 
import java.util.*; 

public class study { 
    public static void main(String[] args) throws IOException { 
     BufferedWriter post = null; 

     File file = new File("text.txt"); //create file 

     if (!file.exists()) 
     { 
       file.createNewFile(); 
     } 

     boolean promptUser = true; 
     FileWriter fileWriter = new FileWriter(file); 

     try { 
      while (promptUser) { 
       post = new BufferedWriter(fileWriter); 
       System.out.println("enter age "); // get age  
       Scanner getage = new Scanner(System.in);   
       int age = getage.nextInt(); 

       if (age < 20 || age > 50){ //age range 
        System.out.println("age must be between 20 and 50"); 
        System.exit(0); 
       } 

       System.out.println("enter name "); //get name 
       Scanner getname = new Scanner(System.in);  
       String name= getname.nextLine(); 

       System.out.println("enter email "); // get email  
       Scanner getarea = new Scanner(System.in);  
       String email= getarea.nextLine(); 

       //send data to file 
       post.write(age + ";"); 
       post.write(name + ";");     
       post.write(email + ";"); 
       post.newLine(); 
       post.flush(); 

       System.out.println("enter quit to quit or any key to continue"); 
       Scanner options = new Scanner(System.in); 
       String option = options.nextLine();  

       if (option.equalsIgnoreCase("quit")) { 
        System.out.println("goodbye!"); 
        post.close(); // close file upon quitting 
        System.exit(0); 
       } 

      } 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }   
    } 
}