2013-07-28 79 views
0

我試圖從.rtf文件打印,但好像我的if語句已損壞。它看起來像分裂不起作用因爲如果我把打印狀態輸出只是作爲第一個打印行,它告訴用戶他們正在搜索哪首歌曲,然後歌曲歌詞是未格式化。除此之外,代碼只是循環while循環。看起來,它沒有找到歌曲,當它擊中if(line.contains(song)){。現在,我只是對文件的位置進行硬編碼,但是當我使用該文件時,我將使該方法使用用戶輸入。bufferedReader未打印文件

任何幫助,將不勝感激。

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

當您在純文本.txt文件而不是.rtf文件上使用它時,該代碼是否工作? – denisw

+0

它的效果更好,反斜槓消失了,但如果我保留上面的代碼,整個文件就會被打印出來而不是停在|處。如果我將其更改爲if(line.toLowerCase()。contains(song)),則它只打印文件的一行,其上帶有歌曲名稱。該文件是這樣組織的「|歌名:歌詞| |其他歌名:歌詞|」。 – Adam

回答

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()); 
     } 
    } 
} 
0
lyricsSearch("/Users/adam/Documents/Final/BlackDahliaMurder/", "miasma.rtf"); 

綜觀lyricsSearch簽名,你似乎是尋找一個名爲「miasma.rtf」的歌 - 我想這是不是你的意圖。 :)

否則,代碼看起來不錯。

+0

我實際上是在一個文件中尋找那首歌。我已經把它改爲.txt,因爲它打印出沒有\ .rtf的地方。問題是,拆分沒有找到我的分隔符。如果我改成這個'if(line.toLowerCase()。contains(song)){',它只打印其中包含歌名的一行。 .txt文件是這樣組織的:「|歌名:歌詞| |其他歌名:歌詞|」。 – Adam