2012-02-06 27 views
0

我正处于学校项目的早期阶段,已经遇到了一个错误。如果我简单地使用for循环导出数组列表内容而没有使用while循环,那么一切正常,但是当我想使用while循环以便我可以提供选项时,输出文本文件不包含任何内容,就像程序一样没有做任何事情,只是创建了一个空文本文件。我很困扰。Printwriter似乎没有输出到指定的文件后,我添加了一个while循环

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.io.PrintStream; 
import javax.swing.JOptionPane; 
import java.util.Arrays; 

public class studentProcessor 
{ 
    public static void main(String[] args) throws FileNotFoundException 
    { 
     PrintStream pr=System.out; 
     String inputFileName = JOptionPane.showInputDialog("Input file:"); 
     String outputFileName = JOptionPane.showInputDialog("Output file:"); 
     File inputFile = new File(inputFileName); 
     Scanner in = new Scanner(inputFile); 
     PrintWriter out = new PrintWriter(outputFileName); 
     ArrayList<Student>student = new ArrayList<Student>(); 
     while (in.hasNextLine()) 
     { 
      student.add(new Student(in.nextInt(), in.next(), in.nextDouble())); 
     } 
     in.close(); 
     boolean done=false; 
     while (!done) 
     { 
      String m=JOptionPane.showInputDialog("Please enter the number for the action which you would like to perform." + 
       "\n1. Add new student(s)\n2. Remove student(s)\n3. Update student's data\n4. Search for student" + 
       "\n5. Sort students\n6. Create GPA report\n7. Output information to a .txt file\n0. Exit"); 
      if (m.equals("1")) 
      { 
       JOptionPane.showMessageDialog(null, "Add new student(s)"); 
      } 
      else if (m.equals("2")) 
      { 
       JOptionPane.showMessageDialog(null, "Remove student(s)"); 
      } 
      else if (m.equals("3")) 
      { 
       JOptionPane.showMessageDialog(null, "Update student's data"); 
      } 
      else if (m.equals("4")) 
      { 
       JOptionPane.showMessageDialog(null, "Search for a student"); 
      } 
      else if (m.equals("5")) 
      { 
       JOptionPane.showMessageDialog(null, "Sort students"); 
      } 
      else if (m.equals("6")) 
      { 
       JOptionPane.showMessageDialog(null, "Create GPA report"); 
      } 
      else if (m.equals("7")) 
      { 
       JOptionPane.showMessageDialog(null, "Output information to a .txt file"); 
       out.println("The students' information is listed below:"); 
       for (Student Stu:student) 
       { 
        out.println(Stu.getId()+" "+Stu.getLName()+" "+Stu.getGpa()); 
       } 
      } 
      else if (m.equals("0")) 
      { 
       System.exit(0); 
       done=true; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, "You have not entered a valid command, try again."); 
      } 
     } 
     out.close(); 
    } 
} 

这里是我正在使用的Student类。

class Student 
{ 
private int id; 
private String lName; 
private double gpa; 

public Student (int studentId, String studentLName, double studentGpa) 
    { 
    id=studentId; 
    lName=studentLName; 
    gpa=studentGpa; 
    } 

public int getId() 
    { return id; } 

public String getLName() 
    { return lName; } 

public double getGpa() 
    { return gpa; } 

public void setId(int id) 
    { this.id=id; } 

public void setLName(String lName) 
    { this.lName=lName; } 

public void setGpa(double gpa) 
    { this.gpa=gpa; } 

} 

感谢您的帮助=]

编辑:这是现在的PrintWriter工作代码的新的部分。感谢所有的帮助!

else if (m.equals("7")) 
     { 
      String outputFileName = JOptionPane.showInputDialog("Designate an output file for student information:"); 
      PrintWriter out = new PrintWriter(outputFileName); 
      out.println("The students are listed below:"); 
      for (Student Stu:student) 
      { 
       out.println(Stu.getId()+" "+Stu.getLName()+" "+Stu.getGpa()); 
      } 
      out.close(); 
     } 
+0

为什么你有System.exit(0);设置为false将导致程序终止并希望正确关闭outfile。 – 2012-02-06 17:31:45

回答

1

你需要写它后刷新您的输出流:

out.flush(); 

此外,你将要创建的输出流,当用户选择你写选项(7)。否则,您将重复将记录写入同一文件。所以更像这样的东西:

 } else if (m.equals("7")) { 
     JOptionPane.showMessageDialog(null, 
       "Output information to a .txt file"); 
     final PrintWriter out = new PrintWriter(outputFileName); 
     out.println("The students' information is listed below:"); 
     for (Student Stu : student) { 
      out.println(Stu.getId() + " " + Stu.getLName() + " " 
       + Stu.getGpa()); 
     } 
     out.close(); 
    } 
+0

继续我的评论从另一个答案,我把out.close()移动到你显示的位置,它现在运行良好。 idk,如果它完全是我放置out.close的位置,或者是因为我移动了out文件的字符串初始化。我会发布一个编辑我做的似乎工作 – 2012-02-06 21:27:55

+0

但是,我想问,PrintWriter之前的'最终'在代码的运行方式上有很大的区别吗?对不起,我是一个新手,这是我第一次尝试这个复杂的项目。 – 2012-02-06 21:33:46

+0

使用final来标记变量可以提升您的程序 - 因为它允许编译器进行某种优化。如果你把它抛开,它不是一个杀手。 – Perception 2012-02-07 00:30:09

1

您是否在关闭它之前尝试flush()ing PrintWriter?

+0

关闭将刷新作家,问题是关闭方法没有被调用。 – 2012-02-06 18:17:11

+0

的确是正确的 - 我没有注意到讨厌的System.exit()。 – 2012-02-06 18:19:35

2

问题是,out.close的行永远不会被调用。

System.exit方法会在关闭打印作业器之前终止您的应用程序。

您应该使用finally finally块来关闭资源。

如果System.exit调用是在方法的结尾而不是在while循环中,那么您的代码也会更清晰。您已经使用布尔值来终止循环,所以让它终止,并继续执行其余的代码。

+0

谢谢指出system.exit()我忘了删除它后,我改变为布尔值。我也想出了我的问题,并正好在我想要使用它而不是在程序开始时在循环内初始化输出文件的字符串。 – 2012-02-06 21:27:46

相关问题