0

我正在制作一个程序,该程序在指定数量的课程中创建成绩日志并将此数据写入文本文件。它写了日期、班级名称、成绩(或者至少应该写)和结束语。由于某种原因我无法弄清楚,我的程序只打印出日期和班级名称。有任何想法吗?

这是代码:

        BufferedWriter data = null; //Writes Output

    try {

        File log = new File ("Grade.txt"); //Name of File
        FileWriter fileWriter = new FileWriter (log, true); //Appends File
        data = new BufferedWriter (fileWriter); //Creates BufferedWriter
        Date date = new Date (); //Sets Date

               //Code here makes an array of class names 

            System.out.print ("Enter Assignment Name:  ");
                title = reader.nextLine ();
                data.write (title);
                data.newLine (); //Blank Line
                reader.nextLine (); //Consume Input
                System.out.print ("Enter Points Possible:  ");
                temp = reader.nextInt();

                if (temp < 0){

                    throw new ArithmeticException ("");

                }else{

                    poss += temp;
                    data.write(poss);

                }//Close If

                System.out.print ("Enter Points Earned");
                temp = reader.nextInt();

                if (temp < 0){

                    throw new ArithmeticException ("");

                }else{

                    earn += temp;
                    data.write(earn);

                }//Close If
4

3 回答 3

0

.close()在程序结束之前,您没有编写器或文件。如果您不关闭编写器,则无法保证其内部缓冲区已刷新到文件和磁盘。

如果您使用的是 Java7,那么您可以使用 try-with-resources 来方便和正确:

try ( FileWriter fileWriter = new FileWriter (log, true); //Appends File
      BuferredWriter data = new BufferedWriter (fileWriter); //Creates BufferedWriter
    )
    {
    data.write("...");
    }
于 2013-03-22T00:51:23.237 回答
0

阅读要点后将阅读器移至下一行。使用 reader.nextInt 时,输入仅读取数字,而不是按 Enter 时创建的换行符。在 reader.nextInt 读取换行符并清除缓冲区之后调用 reader.nextLine。

BufferedWriter data = null; //Writes Output

try {

    File log = new File ("Grade.txt"); //Name of File
    FileWriter fileWriter = new FileWriter (log, true); //Appends File
    data = new BufferedWriter (fileWriter); //Creates BufferedWriter
    Date date = new Date (); //Sets Date

           //Code here makes an array of class names 

        System.out.print ("Enter Assignment Name:  ");
            title = reader.nextLine ();
            data.write (title);
            data.newLine (); //Blank Line
            reader.nextLine (); //Consume Input
            System.out.print ("Enter Points Possible:  ");
            temp = reader.nextInt();
            reader.nextLine(); //added this

            if (temp < 0){

                throw new ArithmeticException ("");

            }else{

                poss += temp;
                data.write(poss);

            }//Close If

            System.out.print ("Enter Points Earned");
            temp = reader.nextInt();
            reader.nextLine(); //added this

            if (temp < 0){

                throw new ArithmeticException ("");

            }else{

                earn += temp;
                data.write(earn);

            }//Close If
于 2013-03-22T00:53:01.777 回答
0

nextInt()方法离开\n(结束行)符号并立即被 拾取nextLine(),跳过下一个输入。你想要做的是使用nextLine()一切,并在以后解析它:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

这是迄今为止避免问题的最简单方法——不要混合你的“下一个”方法。仅使用nextLine(),然后解析ints 或单独的单词。

此外,您应该在完成后始终close()使用BufferedWriter它以节省系统资源。

于 2013-03-22T00:53:55.187 回答