0

我是第一个 Java 编程任务的初学者。对于我们的编程作业,我们将获得一个学生的 .txt 文件,如下所示:

我的问题是:我有一个特定的类,用于将文件中的数据转换为变量,以用于将其打印到屏幕上的不同类。但是,我不知道从输入文件中获取课程编号的变量的好方法,因为该编号不是预先确定的。我能想到的迭代未知数量的唯一方法是使用循环,但这只会覆盖我的变量。另外,老师要求我们不要使用任何 JCL 课程(我真的不知道这是什么意思。)

抱歉,如果我解释得不好,但我想不出更好的方法来概念化它。让我知道我是否可以澄清。

编辑:

public static void analyzeData()
{
    Scanner inputStream = null;

    try
    {
        inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
    }

    catch (FileNotFoundException e)
    {
        System.out.println("File Programming Assignment 1 Data.txt could not be found or opened.");
        System.exit(0);        
    }

    int numberOfStudents = inputStream.nextInt();
    int tuitionPerHour = inputStream.nextInt();
    String firstName = inputStream.next();
    String lastname = inputStream.next();
    String isTuitionPaid = inputStream.next();
    int numberOfCourses = inputStream.nextInt();
    String courseName = inputStream.next();
    String courseNumber = inputStream.next();
    int creditHours = inputStream.nextInt();
    String grade = inputStream.next();

为了展示我现在使用的方法,我只是使用 Scanner 从文件中读取数据,对于 Scanner inputStream,我使用 nextInt() 或 next() 从文件中获取变量。显然,当我不知道每个学生将有多少个班级时,这将不起作用。

4

3 回答 3

1
  1. 创建一个Class called Student

  2. 在类里面使用instance variablelike

    字符串名;

    字符串姓氏;

    Boolean isTuitionPaid; // 布尔值导致 isPaid 为真或假

    字符串[] 课程;

    int creditHours;

    弦乐等级;

  3. 创建此类的构造函数,该构造函数在其参数中采用以下参数

    Student(String fName,String lName,Boolean isPaid,String[] course,int cHours,String gr)

  4. When you read the data of a student from a file, store it in the appropriate data type,如构造函数中所述,然后创建Student类型的Object

  5. 创建Student object with the data, store it an appropriate Collection. 数组列表、地图等

于 2012-06-10T16:42:16.723 回答
0

解析文件,创建一个 POJO(我们称之为模型)并将其存储在适当的集合中(可能 List 的实现会这样做)。顺便说一句,没有人会解决家庭作业,我相信这是这里的政策。

于 2012-06-10T16:24:25.757 回答
-1

也许这会有所帮助:

public static void analyzeData()
{
    try
    {
        Scanner inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
        String str = inputStream.next();
        String[] s = str.split(" ");
        int numberOfStudents = Integer.parseInt(s[0]);
        int tuitionPerHour = Integer.parseInt(s[1]);
        System.out.println("Number of students: " + numberOfStudents);
        System.out.println("Tuition per hour: " + tuitionPerHour + "\n\n");
        for(int i = 0; i<numberOfStudents; i++)
        {
            String str1 = inputStream.next();
            String[] s1 = str1.split(" ");
            String firstName = s1[0];
            String lastName = s1[1];
            int rollNo = Integer.parseInt(s1[2]);
            String isTuitionPaid = s1[3];
            int numberOfCourses = Integer.parseInt(s1[4]);
            System.out.println("Details of student number " + (i+1));
            System.out.println("Name: " + firstName + " " + lastName);
            System.out.println("Roll No: " + rollNo);
            System.out.println("Is Tuition paid: " + (isTuitionPaid == "Y" ? "Yes" : "No"));
            System.out.println("Number of Courses taken: " + numberOfcourses + "\n");
            for(int j = 0; j<numberOfCourses; j++)
            {
                System.out.println("Details of course no " + (j+1));
                String str2 = inputStream.next();
                String[] s2 = str2.split(" ");
                String courseName = s2[0];
                String courseNumber = s2[1];
                int creditHours = Integer.parseInt(s2[2]);
                String grade = s2[3];
                System.out.println("Course Name: " + courseName);
                System.out.println("Course Number: " + courseNumber);
                System.out.println("Credit Hours: " + creditHours);
                System.out.println("Grade: " + grade + "\n");
            }
        }
    }

    catch (FileNotFoundException e)
    {
        System.out.println("File Programming Assignment 1 Data.txt could not be found or opened.");
        e.printStackTrace();
        System.exit(0);        
    }
    catch(IOException ioe)
    {
        System.out.err("IOException has occurred");
        ioe.printStackTrace();
        System.exit(0);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
于 2012-06-10T17:40:04.220 回答