这是包含接收错误消息的方法的驱动程序类,“方法 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);
}
}