【问题标题】:Assigning object values from return of another class' function?从另一个类的函数的返回中分配对象值?
【发布时间】:2012-06-11 04:54:32
【问题描述】:

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

现在,我的驱动程序类中的对象数组应该被分配由 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 吗?

【问题讨论】:

    标签: java function interface static


    【解决方案1】:

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

    【讨论】:

    • 这里所有的 cmets 都指向同一个东西,所以感谢大家的回复,但建议的更改导致:线程“main”中的异常 java.lang.NoSuchMethodError: main
    • @RejectionHurts 更新了我的帖子,将静态添加到您的 main 方法中它会起作用
    • 我怎么会错过这个?!大声笑...我现在觉得自己像个白痴
    • rof... 很高兴您能正常工作... 希望这能解决您的问题
    • @RejectionHurts 请将对您有帮助的答案标记为正确;)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你基本上有两件事:

      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;)应该会消除您面临的错误。

      【讨论】:

        猜你喜欢
        • 2016-02-16
        • 1970-01-01
        • 2013-11-14
        • 2018-12-29
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 2013-12-06
        • 2016-06-24
        相关资源
        最近更新 更多