Given the following class: Song Armstrong artist String title String hat A Wonde
ID: 3716208 • Letter: G
Question
Given the following class: Song Armstrong artist String title String hat A Wonderful World 190 Michael Jackson Thriller durationcint Songl Songlartist String, title String, int duration) Mutator and Accessor methods Write a method named readSongs0 that instantiates an ArrayList of Songs and then populates the ArrayList from a file named kbjams.txt. The format of the input file is shown in the textbox. Basically, it contains an unspecified series of song data items. Each property is on a separate line (Be careful with how you handle the newline character). The method should return the ArrayList as the method return value.Explanation / Answer
public static ArrayList<Song> readSongs() {
try {
ArrayList<Song> data = new ArrayList<Song>();
FileInputStream fin = new FileInputStream("kbjams.txt");
Scanner fc = new Scanner(fin);
while(fc.hasNextLine()){
String artist = fc.nextLine();
String title = fc.nextLine();
int duration = Integer.parseInt(dc.nextLine());
Song s = new Song(artist, title, duration);
data.add(s);
}
fc.close();
return data;
}
catch (Exception e){
e.printStackTrace();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.