繁体   English   中英

从另一个类的函数返回中分配对象值?

[英]Assigning object values from return of another class' function?

因此,在完成最后一次分配后,我被指示接受该代码并将其从命令行参数更改为从文件中读取数据。 一切正常,除了我应该具有一个函数接口的部分,该函数调用文件中的数据,然后执行与以前相同的操作。

现在,应该为我的驱动程序类中的对象数组分配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);
        }
    }
}

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

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

那是因为StudentDAO没有main吗?

另一个编辑

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

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

1-表示即使在我的src文件夹中也找不到数据文件。

2-扫描仪行上也出现错误,我是否应该在文件上不使用扫描仪? 我应该和... DataInputStream一起去吗?

你的方法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

在Lab3类中,创建并实例化StudentDAO,然后阅读以下文本:

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

您基本上有两件事:

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

  2. 您将创建一个由3个元素组成的数组,然后将其丢弃并填充一些新数据。

因此,基本上,您需要替换为:

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

有了这个:

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

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

在您的接口类中替换为: public Object[] readTextData() throws FileNotFoundException; 这样做: public static Object[] readTextData() throws FileNotFoundException; 这将使您的readTextData方法静态。 然后,在DAO中替换方法签名(使用public static Object[] readTextData() throws FileNotFoundException; )类应删除您面临的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM