1

因此,在完成给我的最后一项任务后,我被指示获取该代码并将其从命令行参数更改为从文件中读取数据。一切都很好,除了我应该有一个函数接口的部分,该函数从文件中调用数据,然后做和以前一样的事情。

现在,我的驱动程序类中的对象数组应该被分配由 DAO 类获取的值。DAO 类基于接口。驱动程序类对我尖叫,我创建的对象必须从 DAO 类中的静态函数分配,但该方法不能是静态的......

这次我错过了什么?...

界面:

public interface ScanTextFile {

    public Object[] readTextData() throws FileNotFoundException;
}

DAO类:

public class StudentDAO implements ScanTextFile {

    public Object[] readTextData() throws FileNotFoundException {

        Student[] studentRecord = new Student[3];

        String dataFileName = "data.txt";
        int numberOfRows = 0;

        File dataFile = new File(dataFileName);
        Scanner scan = new Scanner(dataFile);
        int i = 0;
        String delim = "\\|";

        // checks number of rows in data file, making sure there are 3 total
        for(i = 0; scan.hasNextLine(); i++){
            numberOfRows++;
        }
        if(numberOfRows < 3){
            System.err.format((numberOfRows) + " argument(s) - expected 3");
            System.exit(0);
        } else if(numberOfRows > 3){
            System.err.format((numberOfRows) + " arguments - expected 3");
            System.exit(0);
        }

        for(i = 0; i < numberOfRows; i++){
            if(scan.hasNextLine()){
                String temp = scan.nextLine();
                String[] tempData = new String[4];
                Student tempStudent = null;

                for(i = 0; i < tempData.length ; i++){
                    tempData = temp.split(delim);
                }
                System.out.println("DEBUG *** Finished extracting data, creating object...");
                System.out.println("DEBUG Student Data = [�" + temp + "]");

                GregorianCalendar date = new GregorianCalendar();
                try {
                    date = DateUtil.convertFromDMY(tempData[3]);
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }

                tempStudent = new Student(tempData[0], tempData[1], tempData[2], date);
                studentRecord[i] = tempStudent;
            }
        }

        return studentRecord;
    }

}

司机等级:

public class Lab3 { 

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

        Student[] allData = new Student[3];
        allData = (Student[]) StudentDAO.readTextData();

        System.out.println("");
        System.out.println("DEBUG *** Student Objects created, displaying all Students...\n");
        for(Student s : allData){
            Print.print(s);
        }
    }
}

编辑 感谢您指出该错误,谢谢大家,但现在我得到了

线程“main”中的异常 java.lang.NoSuchMethodError: main

那是因为StudentDAO没有main吗?

另一个编辑

@mprabhat 感谢您指出一个非常愚蠢的错误,仍然不知道我怎么没看到><

现在,当扫描仪尝试从文件中读取数据时,我遇到了一个问题。

1 - 说找不到数据文件,即使它在我的 src 文件夹中。

2 - 扫描仪线上也有错误,我不应该在文件上使用扫描仪吗?我应该使用... DataInputStream 吗?

4

3 回答 3

0

您的方法readTextData不是静态的,但您通过使用类名像静态方法一样访问它StudentDAO

StudentDAO.readTextData();

而是创建一个对象StudentDAO,然后调用readTextData

Student[] allData = new Student[3];
StudentDAO studentDAO  = new StudentDAO();
allData = (Student[]) studentDAO.readTextData();

Lab3 中的问题是您的主要方法的签名不正确。

public static void main(String[] args)是正确的签名,您的签名丢失static,因此您得到java.lang.NoSuchMethodError: main

于 2012-06-11T04:58:40.547 回答
0

在课堂 Lab3 中,创建 StudentDAO 的实例,然后阅读如下文本:

StudentDAO dao = new StudentDAO();
allData = (Student[]) dao.readTextData();
于 2012-06-11T04:59:41.967 回答
0

你基本上有两件事:

  1. readTextData()不是静态的,因此您无法以与您相同的方式访问它。您将需要创建一个对象,然后调用该方法。

  2. 您正在创建一个包含 3 个元素的数组,然后丢弃并填充一些新数据。

所以基本上你需要替换这个:

Student[] allData = new Student[3];
allData = (Student[]) StudentDAO.readTextData();

有了这个:

StudentDAO sDao = new StudentDAO();
Student[] students = (Student[])sDao.readTextData();

为了完整起见,如果您执行以下操作,您也应该摆脱错误,但我建议您坚持我上面刚刚列出的方法:

在您的接口类中,将 this: 替换public Object[] readTextData() throws FileNotFoundException;为 this: public static Object[] readTextData() throws FileNotFoundException;。这将使您的readTextData方法静态。替换 DAO (with public static Object[] readTextData() throws FileNotFoundException;) 类中的方法签名应该可以消除您面临的错误。

于 2012-06-11T04:59:54.543 回答