0

私は学校のプロジェクトの初期段階にあり、すでにバグに遭遇しています。whileループなしでforループを使用して配列リストの内容を単純にエクスポートすると、すべてが正常に機能しますが、オプションを提供できるように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; }

}

助けてくれてありがとう=]

編集:これは、現在プリントライターで機能するコードの新しいセクションです。すべての助けに感謝します!

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();
        }
4

3 に答える 3

2

問題は、out.close を含む行が呼び出されないことです。

System.exit メソッドは、印刷ライターを閉じる前にアプリケーションを終了します。

リソースを閉じるには、try finally ブロックを使用する必要があります。

また、System.exit 呼び出しが while ループではなくメソッドの最後にある場合、コードはより明確になります。すでにブール値を使用してループを終了しているので、ループを終了させて​​、残りのコードに進みます。

于 2012-02-06T18:16:39.697 に答える
1

Have you tried flush()ing the PrintWriter before closing it?

于 2012-02-06T17:54:34.987 に答える
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();
     } 
于 2012-02-06T18:07:46.407 に答える