使用扫描仪读取文本文件时出现问题

时间:2013-10-29 17:48:52

标签: java class object

我正在尝试从文本文件中读取数据,但是我无法让它工作。这就是我到目前为止所拥有的

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

    public class Student {
        private String firstName;
        private String lastName;
        private int homeworkScore;
        private int testScore;
        private String letterGrade;
        private int numberOfStudents;

        public Student() {

            numberOfStudents = 0;
            homeworkScore = 0;
            testScore = 0;

        }

        public static void main(String[] args) {

            Scanner scanner = null;
            try {
                scanner = new Scanner(new File("grades.txt"));
            } catch (FileNotFoundException e) {
                System.out
                        .println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
                System.exit(0);
            }
        }

        public void setNumberOfStudents() {
            Scanner scanner = new Scanner(System.in);
            numberOfStudents = scanner.nextInt();

        }

        public void setFirstName() {
            Scanner scanner = new Scanner(System.in);
            String firstName = scanner.next();
        }

        public void setLastName() {
            Scanner scanner = new Scanner(System.in);
            String lastName = scanner.next();
        }
    }

这是我到目前为止的驱动程序类:

public class CourseGrade {

    public static void main(String[] args) {
        Student myStudent = new Student();
        myStudent.setNumberOfStudents();
        myStudent.setFirstName();
        myStudent.setLastName();
    }

}

当我运行驱动程序类时,没有任何反应,所以我不确定它是否实际上将值设置为我想要的值。我知道正在读取该文件,因为我没有收到错误消息,但我不确定这些值是否实际被设置

3 个答案:

答案 0 :(得分:1)

您实际上并未使用扫描仪迭代文件。您所做的就是使用该文件初始化Scanner对象。

您需要执行以下操作:

 while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    //do stuff...
 }

正如您问题的评论部分所指出的,Scanner正在您的Student class main方法中初始化 - 这似乎是错误的。您可以在Student上使用静态方法读取此文件,并根据数据构造Student对象。

答案 1 :(得分:0)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Student {
    private String firstName;
    private String lastName;
    private int homeworkScore;
    private int testScore;
    private String letterGrade;
    private int numberOfStudents;

    public Student() {

        Scanner scanner = null;
        try {
            scanner = new Scanner(new File("grades.txt"));
        } catch (FileNotFoundException e) {
            System.out
                    .println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
            System.exit(0);
        }

        //Assumed file content is as expected
        this.numberOfStudents = scanner.nextInt();
        this.firstName = scanner.next();
        this.lastName = scanner.next();
    }
}

以下驱动程序类将在调用构造函数时打印设置的值。

public class CourseGrade {

  public static void main(String[] args) {

    Student myStudent = new Student();
    //Your requirement is done by now
  }

}

答案 2 :(得分:0)

好的,我让它发挥作用,这就是我所做的:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Student {
    private String firstName;
    private String lastName;
    private int homeworkScore;
    private int testScore;
    private String letterGrade;
    private int numberOfStudents;
    Scanner scanner = null;
    {
        try {
            scanner = new Scanner(new File("grades.txt"));
        } catch (FileNotFoundException e) {
            System.out
                    .println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
            System.exit(0); // Error message is shown when file is not found
        }
    }

    public Student() {
        homeworkScore = 0;
        testScore = 0;
        numberOfStudents = 0;

    }

    public void setNumberOfStudents() {
        numberOfStudents = scanner.nextInt();

    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public void setFirstName() {
        firstName = scanner.next();

    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName() {

        lastName = scanner.next();
    }

    public String getLastName() {
        return lastName;
    }

    public void setHomeworkScore() {

    }

    public int getHomeworkScore() {
        return homeworkScore;
    }

    public void setTestScore() {

    }

    public int getTestScore() {
        return testScore;
    }
}

然后当我运行这个新的驱动程序类时:

public class CourseGrade {

    public static void main(String[] args) {
        Student myStudent = new Student();
        myStudent.setNumberOfStudents();
        myStudent.setFirstName();
        System.out.print(myStudent.getFirstName() + " ");
        myStudent.setLastName();
        System.out.print(myStudent.getLastName());

    }

}

它打印出文本文件中第一个人的名字!我仍然需要设置计算测试和家庭作业得分的方法,但现在似乎都在运行。谢谢你的帮助!