错误消息告诉我一个方法未定义但似乎已定义

时间:2013-09-25 23:52:39

标签: java methods undefined

这是驱动程序类,其中包含接收错误消息的方法,“方法ReadSongArray(File,int)未定义类型SongArray。”我不确定这里出了什么问题,因为我确保在我的驱动程序类中创建一个SongArray类型的对象。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ArrayDriver {

    public void main(String[] args){
        File file1 = new File("TenKsongs.csv");
        SongArray drive = new SongArray();
        drive.ReadSongArray(file1, 10);
    }
}

这是SongArray课程。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SongArray {

public Song[] ReadSongArray(File file, int numsongs){
    File file1=new File("TenKsongs.csv");
    Song[] songDB;
    songDB=new Song[numsongs];
    int i=0;
    try{
        FileReader file_reader=new FileReader(file1);
        BufferedReader buf_reader = new BufferedReader (file_reader);
        while(i<numsongs){
            String line=buf_reader.readLine();
            String[] data=line.split(",");// in csv file, attributes are separate using ","
            //transfer string to float and int
            float duration_StrF=Float.parseFloat(data[3]);
            int digitalid_StrInt=Integer.parseInt(data[4]);

            String title_rmSP=data[1].replaceAll("\\s+", "");//remove spaces in song title and artist name
            String artist_rmSP=data[2].replaceAll("\\s+", "");


            Song chips = new Song(title_rmSP,artist_rmSP,duration_StrF,digitalid_StrInt);

            i++;
        }
        buf_reader.close();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    return (songDB);
}

}

3 个答案:

答案 0 :(得分:1)

您可能正在使用类路径中没有该方法的旧版本。尝试保存源代码文件,重新编译,重新部署并重新启动服务器。

这样的事情会让开发人员感到疯狂。

答案 1 :(得分:0)

您遇到的问题是因为如果要将其作为对象实例,SongArray类缺少一些组件。您应该阅读面向对象编程,但是通过调用new SongArray()告诉Java创建SongArray的实例您需要在类中添加构造函数来执行此操作,除非您正在创建static 1}}你不创建实例的类,而是通过从不调用new来传递参数。

public class SongArray
{
    //public or private components
    private Song[] songDB;
    private int i;

     //default constructor 
    public SongArray()
     {
        //build components of the SongArray object such as:
        songDB = new Song[100];
     }
     //overloaded constructor
     public SongArray(int count, Song[] songs)
     {
          songDB = songs;
          i = count;
    }
    //other components of class and various functions such as:
    public Song[] readSongArray(File f, int numsong)
    {
        //do stuff here like in your question
    }

}

如果您不想或不能实例化它们,您应该创建静态类。

您可以在此处直接从Java / Oracle了解有关OOP的更多信息:http://docs.oracle.com/javase/tutorial/java/concepts/

答案 2 :(得分:0)

您可以尝试做的是确保所有类都在同一个包中,以避免名称空间混淆。

创建一个新包 例如。 com.test.mike

将所有文件复制到此包中。因此,他们将在您的课程路径上引用如下  com.test.mike.SongArray
 com.test.mike.ArrayDriver
 com.test.mike.Song

相关问题