Search code examples
printingsplitbufferedreader

bufferedReader isn't printing the file


I'm trying to print from a .rtf file but it seems like my if statement is broken. It sort of seems like the split isn't working 'cause if I put a print stateThe output just comes out as the first print line, which tells the user which song they're searching for and then the song lyrics that are unformatted. Other than that, the code just loops through the while loop. It appears that it's not finding the song when it hits if(line.contains(song)){. For now, I'm just hardcoding the file's location in but when I get it to work, I'll make the method use user input.

Any help would be appreciated.

public static void main(String[] args){
     lyricsSearch("/Users/adam/Documents/Final/BlackDahliaMurder/", "miasma.rtf");
}

public static void lyricsSearch(String artist, String song){
    try {
        String stringSearch = song;
        // Opens the album file as a buffered reader
        BufferedReader bf = new 
        BufferedReader(new FileReader("/Users/adam/Documents/Final/BlackDahliaMurder/miasma.rtf"));

        // Let the user know what we are searching for
        System.out.println("Searching for " + song + "...");

        // Loop through each line, parsing.
        String line;

        while (( line = bf.readLine()) != null){
            System.out.println(line);
            if (line.contains(song)){
                String[] songInfo = line.split("\\|");
                System.out.println(Arrays.toString(songInfo));
            }       
        }
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

Solution

  • The file organization has to be correct. All songs on an album were on one file. The songs have to start with a pipe and each line has to end with another pipe. The key is to not have any line breaks in the file.

    import java.util.Arrays;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Experiment {
    
    private int artist;
    private String album;
    private String song;
    private String aaa = "/Users/adam/Documents/Final/AngelsAndAirwaves/";
    private String bdm = "/Users/adam/Documents/Final/BlackDahliaMurder/";
    private String naf = "/Users/adam/Documents/Final/NakedAndFamous/";
    private String awn = "/Users/adam/Documents/Final/AWOLNATION/";
    
    
        public static void lyricsSearch(int artist, String album, String song) {
            String userArtistChoice = null;
            album.toLowerCase();
            song.toLowerCase();
            try {
    
                if (artist == 0){
                    userArtistChoice = "/Users/adam/Documents/Final/AngelsAndAirwaves/";
                }
                else if (artist == 1){
                    userArtistChoice = "/Users/adam/Documents/Final/BlackDahliaMurder/";
                }
                else if (artist == 2){
                    userArtistChoice = "/Users/adam/Documents/Final/NakedAndFamous/";
                }
                else if (artist == 3){
                    userArtistChoice = "/Users/adam/Documents/Final/AWOLNATION/";
                }
                else
                    System.out.println("that i'n't right....");
    
                String stringSearch = song;
                // Opens the album file as a buffered reader
                BufferedReader bf = 
                    new BufferedReader(new FileReader(userArtistChoice + album + ".txt"));
    
                // Let the user know what we are searching for
                System.out.println("Searching for " + song + "...");
    
                String line;
                //loop through each line, parsing     
                while ((line = bf.readLine()) != null){
                    //print the song.  each | delimits a string.  song stops at carriage return.
                    if (line.toLowerCase().contains(song)){
                        String[] songInfo = line.split("\\|");
                        for(String el : songInfo) {
                        System.out.println(el);
                    }
    
                }   
            }
            bf.close();
            }
            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }