2014-10-29 58 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(); 
     }   
    } 
}