0

The purpose of this program is to take information from a file about a music collection and turn it into three arrays.

>4
>141 Pure Heroine:Lorde
>171 Lights Out:Ingrid Michaelson
>270 Unorthodox Jukebox :Bruno Mars
>190 Head Or Heart:Christina Perri

In the file, the 4 stands for how long the arrays will be, the numbers are one array, the album titles another, and the artist names are the final array. The arrays for the titles and artist names are separated by the colon. While I can create the array for the numbers, what is giving me trouble is how to create the separate arrays for name and titles. I understand that I have to convert it into a sting and use the colon as a delimiter but I'm unsure as to how.

import java.util.*;
import java.io.*;

public class tunes {

public static void main(String[] args) throws FileNotFoundException {
    int size; //Determines the size of the arrays
    Scanner input = new Scanner(new File("music.txt"));
    size = input.nextInt();
    int[] time = new int[size];
    for (int i = 0; i < time.length; i++) { // Creates an array for the numbers
        time[i] = input.nextInt();
        input.nextLine();
    }
    String[] artist = new String[size]; 
    for (int i = 0; i <artist.length; i++) { 
        while (input.hasNextLine()){

        }
     } 
    System.out.println();
    System.out.println("TOTAL TIME\t\t\t\t" + calcTotalTime(time));
    System.out.println();
    System.out.println();
    System.out.println("LONGEST TRACK");
    System.out.println("-------------");
    System.out.println();
    System.out.println();
    System.out.println("SHORTEST TRACK");
    System.out.println("--------------");

}

public static void printTable(int[] time, String[] artist, String[] title) {
    System.out.println("TITLE\t\t\t" + "ARTIST\t\t\t    " + "TIME");
    System.out.println("-----\t\t\t" + "------\t\t\t    " + "----");
    for (int i = 0; i < time.length; i++) {
        System.out.println(title[i] + "\t" + artist[i] + "\t" + time[i]);
    }
}

public static int calcTotalTime(int[] time) {
    int sum = 0;
    for (int i = 0; i < time.length; i++) {
        sum = sum + time[i];
    }
    return sum;
}

public static int findLongest(int[] time) {
    int longest = 0;
    for (int i = 1; i < time.length; i++) {
        if (time[i] > time[longest]) {
            longest = i;
        }
    }
    return longest;
}

public static int findShortest(int[] time) {
    int shortest = 0;
    for (int i = 1; i < time.length; i++) {
        if (time[i] < time[shortest]) {
            shortest = i;
        }
    }
    return shortest;
}
}

An example of how the output would look like would be

>Pure Heroine       Lorde              141
>Lights Out         Ingrid Michaelson  171
>Unorthodox Jukebox Bruno Mars         270
>Head or Heart      Christina Perri    190
Bursi
  • 3
  • 1

3 Answers3

2

You can use String.split(":") on your text to split the artist/title Strings into String[] arrays.

For instance:

System.out.println(Arrays.toString("Head Or Heart:Christina Perri".split(":")));

Output

[Head Or Heart, Christina Perri]
Mena
  • 47,782
  • 11
  • 87
  • 106
1

Use the String split() method to save the pieces into a new array. The split() method takes a regular expression - see this question about how to use it. The colon is not a special character in a RegExp and therefore does not need to be escaped.

Community
  • 1
  • 1
armadadrive
  • 963
  • 2
  • 11
  • 42
0

Allocate all the arrays first after reading the length of the arrays, then parse each line in one go using split to tokenize the artist-title part of the line.

size = input.nextInt();
int[] time = new int[size];
String[] artist = new String[size];
String[] title = new String[size];

for (int i = 0; i < time.length; i++) {
        time[i] = input.nextInt();
        String[] parts = input.next().split( ":" );
        artist[i] = parts[0];
        title[i] = parts[1];
}
stridecolossus
  • 1,449
  • 10
  • 24