如何让我的程序加载将与我的其他 类 通信的文件?

How do I get my program to load a file that will be communicate with my other classes?

好的,所以我正在创建一个程序,它将根据用户输入的内容进行组织和显示。用户将加载一个文件然后使用这些命令。用户只能加载逗号分隔值文本文件。第一行将包含单词 "name",然后是 class 中所有作业的名称,以逗号分隔。第二行将给出 class 的名称,后跟每个 assignment.Any 行可能的分数,然后是学生姓名,后跟每个作业的分数。

For example:
name,essay 1,test 1,essay 2,test 2,final
Philosophy 101,5,20,5,20,50
Aristotle,4,18,3,15,40
Euclid,3,15,2,10,35
Immanuel Kant,4,18,4,20,48
Ben Franklin,5,20,4,19,49

程序将以 "command" 模式启动,允许用户键入各种命令并从系统获取结果。命令是:

1. exit
Causes the program to exit.
2. help
Causes the program to print a list of commands accepted by the system.
3. load [filename]
Causes the program to load a class database specified in the file [filename]. If a database is
currently loaded, the program should discarded it and replace it with the new one.
4. students
Prints out a list of students from the class, along with total points, and final grades (A, B, C, D,
F) for each student. These should be properly aligned in columns, so that the grades line up
above one another, and the student names are left aligned.
5. assignments
Prints out a list of assignments from the file, along with how many points are possible for each
assignment. As with students, the assignments should be properly aligned on columns.
6. student [student name]
Prints a report for the student. If the student does not exist, it prints a message stating that the
student does not exist, and continues without crashing. The report should contain the following
information:
1. Student name
2. A list of all assignments, and the grades the student achieved for them and points possible
3. Overall points made in the course and total points possible.
4. Final letter grade for the student, given the grading scale
A >= 90%
B >= 80%, < 90%
C >= 70%, < 80%
D >= 60%, < 70%
F < 60%
5. The report should be formatted reasonably, so that assignments and grades line up in
columns, for example.
7. assignment [assignment name]
Prints a report about a particular assignment, which includes the following information:
1. The assignment name, and points possible
2. The low, high, and average for the assignment
3. How many students achieved different grades (A-F) for the assignment.
8. grades
Prints a report about overall grades in the class. The report must include the following
information:
1. low, high, and average percentages in the class
2. how many students achieved the different grades in the class (A-F)

现在让我困惑的是负载class。它基本上决定了其他 classes 的信息,但我没有多少运气让它发挥作用。到目前为止,这是我所有的代码。

import java.util.*;
import java.io.*;
public class program7
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Grade Stats by ");
        System.out.print(">");
        while(scan.hasNextLine())
        {

            String input = scan.nextLine();

            if(input.equals("exit"))
            {
                System.exit(0);
            }
            else if(input.equals("help"))
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
            else if(input.contains("load"))
            {
                String[] split = input.split(" ");
                Load load = new Load(split[1]);
                System.out.print(">");
            }
            else if(input.equals("students"))
            {

                System.out.print(">");
            }
            else if(input.equals("assignments"))
            {

                System.out.print(">");
            }
            else if(input.contains("student"))
            {
                String[] split = input.split(" ");
                Student student = new Student(split[1]);
                System.out.print(">");
            }
            else if(input.contains("assignment"))
            {

            }
            else if(input.equals("grades"))
            {

            }
            else
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
        }
    }

}

那是我的主要 class 顺便说一句。这是其他 classes.

import java.util.*;
import java.io.*;
public class Load
{
    public String inputFile;
    public List<String> info = new ArrayList<String>();

    public Load(String inputFile)
    {

        this.inputFile = inputFile;
    }
    public void getFileContent()
    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(inputFile));
            try
            {
                String line =  "";

                while(in.readLine() != null)
                {
                    line = in.readLine();
                    info.add(line);

                }


            }
            catch(IOException e)
            {
                System.err.println("Exception, man");
            }
            finally
            {
            in.close();
            }
        }
        catch(FileNotFoundException e)
        {
            System.err.println("File wasnt found bro");
        }
        catch(IOException e)
        {
            System.err.println("Exception, man");
        }
    }

}

那是我的负担 class 下一个是我的学生 class

public class Student
{
    public Student()
    {

    }

}

我无法让 Load 工作,所以我没有过多地处理这些。这是学生 class.

import java.util.*;
import java.io.*;
public class Student
{

    private String student;
    public Student(String student)
    {
        this.student = student;
    }

    public String Splitter()
    {

        return "";

    }
    public String Printer()
    {

        return "";
    }

}

现在我知道我必须有一个构造函数并通过在主 class 中创建它的实例来调用 class,但它似乎不是 运行在我的 Load class 中使用我的 void 方法。有什么办法可以解决这个问题吗?如果有任何更好的想法我想听听,我只是在想一种方法,我可以轻松地将信息(用户输入文件)传输到我所有的 classes,这样它们就可以按照用户的意愿进行操作,或根据他或她的输入。 哦是的,这个程序只是 运行 在命令行上。

看起来你从未在 main 中调用 getFileContent()。应该看起来像这样:

else if(input.contains("load"))
{
    String[] split = input.split(" ");
    Load load = new Load(split[1]);
    load.getFileContent();    // forgot to call
    System.out.print(">");
}

如果提供的文件有效,应该有内容添加到加载class中指定的ArrayList。