从文本文件中读取数据打印到终端和新文本文件

时间:2015-10-26 22:24:51

标签: java text-files printwriter

我想从文本文件中读取数据并将输出打印到终端和文本文件。我有一个循环读取数字和一个读取非数字字符,但没有任何东西打印到终端。我是编程新手。 顺便说一下,我正在将旧项目转变为新项目。

package studenttester;


public class Student
{
    private String name;
    double quizScore;
    double quizAverage;
    private int numQuizzes;
    String grade;


  /**
 * Returns the name of a student
 * 
 * @return the name of the student 
 */
     public Student (String inName)
    {
            name = inName;
            quizAverage = 0;
            quizScore = 0;
            numQuizzes = 0;
    }
    public String getName()

    {
        return name;
    }

 /**
 * Adds a quiz score to the total quiz score of the student
 * 
 * @param score the score of a quiz     
 */
      void addQuiz(int score)
    {

        numQuizzes += 1; 
        quizScore += score; 

    } 

/**
 * Returns the total score of all the quizzes combined that student took
 * 
 * @return the value of score
 */


    double getTotalScore()
    {

        return quizScore; 

    } 


/**
 * Returns the average score of all the quizzes a student took
 * 
 * @return
 */
    double getAverageScore() 
    {

        return quizScore / numQuizzes; 

    } 
}


package studenttester;


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



public class StudentTester {


public static void main(String[] args) throws FileNotFoundException
{

    System.out.println("Student Name         Number of Quizzes                       Average");

    Scanner reader = new Scanner(new File("quizScores.txt"));

    String studentName = "", first="", last="";
 while (!reader.hasNext("-10"))
{

}
while (reader.hasNextDouble())
{
        first = first+reader.next();
        studentName = last + first;
}

Student newStudent = new Student(studentName);

while (reader.hasNextDouble() && !reader.hasNext("-10"))
    {
    System.out.printf("");
    }

{
           // writer.close;
}


}
    }

1 个答案:

答案 0 :(得分:0)

"红色下划线"我猜是指编译器错误。 看看你的while循环,似乎是错误的。 试试......

while (reader.hasNextDouble() && (!reader.hasNext("-10")))
    System.out.printf("");
}
reader.close();

代替。