如何从其他课程中调用列表?

时间:2015-04-18 19:38:40

标签: java class arraylist compiler-errors bufferedreader

好的,所以我正在建立一个程序,用于对学生的成绩和记录进行排序。它是一个命令行程序,运行时首先要求用户输入。有几个命令,如退出(退出程序),加载[文件名](加载文件名),学生[学生姓名](加载学生记录)等,其他命令并不重要。好吧,基本上我想知道的是什么,我坚持的是所有那些函数将在单独的类中,并将在用户输入特定命令时调用,但如果我把“load”命令放在自己的类中,那么怎么做我得到它与其他类共享其信息?我知道我必须使用BufferReader来读取文件,但是我将如何实现我的加载类,或者如果有更好的方法可以自由地说出来。到目前为止,这是我的代码。我的其他课程并不多,因为我觉得我需要弄清楚如何阅读并首先与其他课程共享文件。

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(" ");
                LoadStudents loadStudents = new LoadStudents(split[1]);
                loadStudents.getFromFile();
                System.out.print(">");
            }
            else if(input.equals("students"))
            {
                Students students = new Students();
                students.printer();

                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(">");
            }
        }
    }

}

这是我的主要课程,但这是我的Load和Students课程。

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

    public LoadStudents(String inputFile)
    {

        this.inputFile = inputFile;
    }
    public List<Object> getFromFile()
    {
        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 ");
        }
        catch(IOException e)
        {
            System.err.println("Exception, man");
        }
        return info;
    }

}


import java.util.*;
public class Students
{
    public Students()
    {

    }
    public void printer()
    {
        List<Object> info = (new LoadStudents()).getFromFile();
        for (int x = 0; x<info.size(); x++)
        {
            System.out.println(info.get(x));
        }
    }

}

学生班没有完成,但我想弄清楚如何阅读其他班级的列表。我已经做过研究并且已经看到了3个类似的问题,但是他们仍然缺少一些因为我不断收到错误

.\Students.java:11: error: constructor Load in class Load cannot be applied to g
iven types;
                List<Object> info = (new LoadStudents()).getFromFile();
                                     ^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

我理解它想要输入,但是我希望它在输入命令“input [whateverfile]”时使用用户给出的先前输入。 任何人都可以告诉我如何将我的Load类生成的列表调用到任何其他类?

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。我的建议是你的Load类应该是一个实际上从文件中创建Student列表的工厂,而不是字符串列表。

以下是更多建议:

  • Load类可以具有所有方法静态,并且它可以是不可实例化的。读取文件后,此类中的字段无用,您可以将它们作为参数传递给静态方法。
  • Load不是该类的好名字。 LoadStudents更有意义,甚至更好StudentFactory
  • LoadStudent可能不需要公开,包私有就足够了。这些类中的所有方法都相同。始终使用尽可能低的能见度。
  • data()也不是该方法的好名字。类似getStudents()的内容,或者如果您遵循上述建议getFromFile(),则更有意义。
  • 始终打印/记录例外的堆栈跟踪,否则您甚至不知道它发生了什么/发生在哪里。
  • 不是让用户输入整个命令,而是在每个选项上加上一个数字,让用户按编号选择,输入的速度更快,同时避免拼写错误。
  • 只导入您实际使用的类而不是整个软件包,这将使编译更快(除非您从该软件包导入大量类,这不是这里的情况)

编辑:既然你仍然不明白我的意思,这里有一个例子:

class StudentFactory {

    private static List<Student> listCache = new ArrayList<>();

    static List<Student> getStudents(final String filePath) {
        if (listCache.isEmpty() {
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(filePath));
                String line;
                while((line = in.readLine()) != null) {
                    // Parse the line and create a Student instance from it, then add it to the list
                    listCache.add(student);
                }
            } catch(IOException e) {
                System.err.println("Exception, man");
                e.printStackTrace();
            } catch(FileNotFoundException e) {
                System.err.println("File wasnt found ");
                e.printStackTrace();
            } finally {
                if (in != null) in.close();
            }
        }
        return listCache;
    }

    private StudentFactory() {
        // Avoid instantiation
    }
}

然后你可以做

final List<Student> listOfStudents = StudentFactory.getStudents(filePath);

从代码中的任何位置获取学生列表。如果您之前已经通过它,则filePath可以为null。

相关问题