0

我正在尝试从 .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());
    }
}
4

2 回答 2

0

文件组织必须正确。一张专辑中的所有歌曲都在一个文件中。歌曲必须以管道开始,每行必须以另一个管道结束。关键是文件中没有任何换行符。

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());
        }
    }
}
于 2013-07-30T17:37:41.183 回答
0
lyricsSearch("/Users/adam/Documents/Final/BlackDahliaMurder/", "miasma.rtf");

查看 的签名lyricsSearch,您似乎在搜索一首名为“miasma.rtf”的歌曲——我猜这不是您的本意。:)

否则,代码看起来不错。

于 2013-07-28T18:29:20.037 回答