Printing bufferedReader不是';不要打印文件

Printing bufferedReader不是';不要打印文件,printing,split,bufferedreader,Printing,Split,Bufferedreader,我试图从一个.rtf文件打印,但似乎我的if语句被破坏了。似乎分割不起作用,因为如果我输入一个打印状态,输出只是作为第一行打印出来,它告诉用户他们正在搜索哪首歌,然后是未格式化的歌词。除此之外,代码只是在while循环中循环。当它点击if(line.contains(song)){时,似乎找不到歌曲。现在,我只是在硬编码文件的位置,但当我让它工作时,我会让该方法使用用户输入 任何帮助都将不胜感激 public static void main(String[] args){ lyric

我试图从一个.rtf文件打印,但似乎我的if语句被破坏了。似乎分割不起作用,因为如果我输入一个打印状态,输出只是作为第一行打印出来,它告诉用户他们正在搜索哪首歌,然后是未格式化的歌词。除此之外,代码只是在while循环中循环。当它点击
if(line.contains(song)){
时,似乎找不到歌曲。现在,我只是在硬编码文件的位置,但当我让它工作时,我会让该方法使用用户输入

任何帮助都将不胜感激

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());
    }
}
看着
lyricsSearch
的签名,你似乎在寻找一首名为“miasma.rtf”的歌曲——我想这不是你的本意


否则,代码看起来很好。

文件组织必须正确。相册上的所有歌曲都在一个文件中。歌曲必须以管道开头,每行必须以另一管道结尾。关键是文件中不能有任何换行符

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());
        }
    }
}

当您在纯文本.txt文件而不是.rtf文件上使用它时,代码是否工作?它工作得更好,因为反斜杠消失了,但是如果我将代码保持在上面的状态,整个文件将被打印出来,而不是停在|处。如果我将其更改为if(line.toLowerCase().contains(song)){`它只打印带有歌曲名称的文件的一行。文件的组织方式如下“|歌曲名称:歌词| |其他歌曲名称:歌词|”。我实际上是在文件中查找那首歌。后来我将其更改为.txt,因为它没有打印出\where.rtf。问题是,split找不到我的分隔符。如果我更改为this
If(line.toLowerCase().contains(song)){
,它只打印其中包含歌曲标题的一行。txt文件是这样组织的“|歌曲名称:歌词| |其他歌曲名称:歌词|”。
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());
        }
    }
}