Search code examples
javabufferedreaderreadlinefilereader

Java BufferedReader readline calculate values from file


I am new to Java and at the moment lost.

I have this code:

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

/**
 *
 * @author Darwish
 */
public class M3UReader {

    /**
     * @param args the command line arguments
     */

    public static boolean isValidHeader(String playList)
    {
        boolean returnValue = false;
        BufferedReader br;
        try
        {
            br = new BufferedReader(new FileReader(new File(playList)));
            String s = br.readLine(); // declares the variable "s"
            if(s.startsWith("#EXTM3U")) { // checks the line for this keyword
                returnValue = true; // if its found, return true
            }
            br.close();
        }
        catch (Exception e)
        {
            System.err.println("isValidHeader:: error with file "+ playList + ": " + e.getMessage());
        }

        return returnValue;
    }
    public static int getNumberOfTracks(String playList)
    {
        int numberOfTracks = 0; // sets the default value to zero "0"
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(new File(playList)));
            String s;
            while((s = br.readLine())!=null) // if "s" first line is not null
            {
                if(s.startsWith("#")==false) { // if the first line starts with "#" equals to false. 
                    numberOfTracks++; // increments
                }
            }
            br.close();
        }
        catch (Exception e)
        {
            numberOfTracks = -1; // chek if the file doesnt exist 
            System.err.println("could not open/read line from/close filename "+ playList);
        }
        return numberOfTracks;

    }

    public static int getTotalMinutes(String playList)
    {
        // code needed here
    }

    public static void main(String[] args) {
        // TODO code application logic here
        String filename = "files\\playlist.m3u"; // finds the file to read (filename <- variable declaration.) 
        boolean isHeaderValid = M3UReader.isValidHeader(filename); // declares the variabe isHeaderValid and links it with the class isValidHeader
        System.out.println(filename + "header tested as "+ isHeaderValid); // outputs the results

        if(isHeaderValid)
        {
            int numOfTracks = M3UReader.getNumberOfTracks(filename);
            System.out.println(filename + " has "+ numOfTracks + " tracks ");
        }

    }
}

On the method getTotalMinutes, I have to find a way to calculate the totals of the int values that was read from the file. The File has this data:

#EXTM3U
#EXTINF:537,Banco De Gaia - Drippy F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\01 Drippy.mp3
#EXTINF:757,Banco De Gaia - Celestine F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\02 Celestine.mp3
#EXTINF:565,Banco De Gaia - Drunk As A Monk F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\03 Drunk As A Monk.mp3
#EXTINF:369,Banco De Gaia - Big Men Cry F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\04 Big Men Cry.mp3

The number after the #EXTINF: is the length of the music which from the data above is in seconds.

I don't know what code to write on the getTotalMinutes method to get the program to read the minutes from the file and then calculate all of them to get the total of minutes. I searched the web on how to do this unfortunately I can't find any. So any help is appreciated.


Solution

  • You can use this, its just copy of your getNumberTracks method but it is parsing the file the way you need to get total minutes :

    public static final String beginning = "#EXTINF:";
    public static final String afterNumber = ",";
    
    public static int getTotalMinutes(String playList) {
        int value = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(playList)));
            String s;
            while ((s = br.readLine()) != null) // if "s" first line is not null
            {
                if (s.contains(beginning)) {
                    String numberInString = s.substring(beginning.length(), s.indexOf(afterNumber));
                    value += Integer.valueOf(numberInString);
                }
            }
            br.close();
        } catch (Exception e) {
        }
        return value;
    }