I've read the text file into 3 different arrays because the file data has to be separated according to the exercise stipulations. I'm now having an issue with retrieving it and formatting it into a table. I'm not sure where I'm making the mistakes.
/* muTunes program processes a sampling of my music collection in
* my library. The program reads the data into three arrays
* (2 Strings for title and artist, and 1 integer for song length)
* from my text file. The songs will be output in a formatted table,
* table, indicating total of all songs, the longest song, the
* shortest song, and the song titles in alphabetical order.
*/
import java.io.*;
import java.util.*;
public class muTunes {
// Read file and call methods to proceed
public static void main(String[] args) throws FileNotFoundException {
Scanner fileScanner = new Scanner(new File("Music.txt"));
readSongs(fileScanner);
//printTable();
//findSongs();
//alphaSongTitles();
}
public static void readSongs(Scanner fileScanner) {
int numOfSongs = fileScanner.nextInt();
fileScanner.nextLine();
while (fileScanner.hasNextLine()) {
String songLine = fileScanner.nextLine();
String delims = "[:]";
int[] times = new int[10];
String[] titles = new String[10];
String[] artists = new String[10];
String[] tokens = songLine.split(delims);
for (int i = 0; i < tokens.length - 2; i++) {
String songTimes = tokens[0];
// System.out.print(tokens[i]+" "); //testing tokens
// System.out.print(songTimes + " "); //testing songTimes
times[i] = Integer.parseInt(songTimes);
//System.out.print(times[i] + " "); // test contents of array
}
for (int i = 1; i < tokens.length - 1; i++) {
String songTitles = tokens[1];
titles[i] = songTitles;
//System.out.print(titles[i] + " "); // test contents of array
}
for (int i = 2; i < tokens.length; i++) {
String songArtists = tokens[2];
artists[i] = songArtists;
//System.out.print(artists[i] + " "); // test contents of array
}
int totalTime = 0;
for(int songSelect = 1; songSelect <= numOfSongs; songSelect++){
totalTime += times[i];
}
printTable(titles, artists, times);
}
}
public static void printTable(String[] titles, String[] artists, int[] times){
System.out.printf("%-5s%,24s%,24s\n%-5s%,24s%,24s\n", "TITLE", "ARTIST", "TIME", "-----", "------", "----");
System.out.printf("%-5s%,24s%,24s\n", titles, artists, times);
System.out.println();
System.out.println("%-10s%48d\n", "TOTAL TIME", totalTime);
}
}