2016-12-13 80 views
-1

我想給用戶他們的每日星座運勢一個段落。我有一個大的文本文件,它看起來有點像這樣:使用的BufferedReader讀從文本文件

白羊座

當你身邊有從來沒有一個沉悶的時刻[繼續占星] [繼續占星] [繼續占星] [繼續占星] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [結束]。

牛牛

漸入[繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座] [繼續星座運勢] [繼續星座運勢] [繼續星座運勢] [繼續星座運勢] [繼續星座運勢] [繼續星座運勢] [結束]。

此文本文件中包含了所有十二生肖的所有星座。我試圖根據用戶的輸入打印出正確的段落。有人可以幫助我嗎?這是我到目前爲止:

public static void getDailyHoroscope() throws IOException{ 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please input your zodiac sign:"); 
    String sign = sc.nextLine(); 
    String file = ".txt file"; 

    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 

     while ((line = br.readLine()) != null) { 
      System.out.print(line); 
     } 
     br.close(); 
    } 
    catch(FileNotFoundException e) { 
     System.out.println("Cannot find" + file); 
} 

我不知道如何繼續。這隻會吐出控制檯中的整個文本文件。我想根據用戶的符號來分段輸出。

+0

一個文件包含所有星座的名字呢?白羊座,金牛座......' – toto

+1

你究竟有沒有嘗試過?這是讀取和打印文本文件的代碼。關於如何只打印出你想要的部分,你有什麼想法? –

+0

修復你的大括號在異常處理程序上。另外,請考慮使用try-with-resources:'try(BufferedReader br = new BufferedReader(new FileReader(file))){'。 –

回答

-1

試試吧:

Scanner sc = new Scanner(System.in); 
     System.out.println("Please input your zodiac sign:"); 
     String sign = sc.nextLine(); 
     String file = ".txt file"; 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 

      String sign2 = ""; 
      String content = ""; 
      String nl = System.getProperty("line.separator"); 
      //I assummed that your text not end with [end] for each sign zodiac. 
      while ((line = br.readLine()) != null) { 
       String l = line.trim().toLowerCase(); 
       //Check if line is equal than some zodiac sign. 
       String sign3 = ""; 
       if (l.equals("aries")) { 
        sign3 = "aries"; 
       } else if (l.equals("tauro")) { 
        sign3 = "tauro"; 
       } //complete here all zodiac sing with else if. 

       //Check if sign is equal than sign enter by user and data content for this sign still not ready. 
       if (sign2.length() == 0 && sign3.length() != 0 && sign.toLowerCase().equals(sign3)) { 
        sign2 = sign3; 
       } else { 
        //Check if we need concatenate data for this sign. 
        if (sign2.length() > 0 && sign3.length() == 0) { 
         content += line + nl; 
        } else { 
         //If this line is equal other sign and content data we finalized and break. 
         if (sign2.length() > 0 && sign3.length() != 0) { 
          break; 
         } 
        } 
       } 

      } 
      br.close(); 

      System.out.println(content); 
     } catch (Exception ex) { 
      System.out.println("Cannot find" + file); 
     } 
+1

僅有代碼的答案很少如此理想。考慮添加一個解釋,說明爲什麼它應該適用於OP。 – rafid059

2

好像你已經做了很多工作。

在你的情況下,它可能是一個商品的想法,首先閱讀整個文件,按段落分割,將其存儲到一些集合,如Map<Sign, Paragraph>Set<Paragraph>,然後在該集合中搜索需要的條目。

0

我的建議是繼續讀文件,直到你擊中了用戶的輸入,後跟一個空行,並繼續讀書,直到你打另一個新行後跟一個空行。

像這樣的東西可能會奏效:

public static void getDailyHoroscope() throws IOException{ 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please input your zodiac sign:"); 
    String sign = sc.nextLine(); 
    String file = ".txt file"; 

    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     boolean print = false; 
     while ((line = br.readLine()) != null) { 
      //If our print flag is false and we found a line that's just our users's sign 
      if((!print) && line.trim().equalsIgnoreCase(sign)){ 
       //set our print flag to true 
       print = true; 
      } 
      if(print){ 
       //if we're printing, and the line we just read was completley empty, unset our print flag. 
       if(line.trim().isEmpty()){ 
        print = false; 
       } 
       System.out.println(line); 
      } 
     } 
     br.close(); 
    }catch(FileNotFoundException e) { 
     System.out.println("Cannot find" + file); 
    } 
} 

請記住我在記事本寫了這個,可能無法正常工作。

考慮到你的上述帖子的一些考慮因素: 在這段代碼的當前狀態下,如果它甚至可以運行,它可能只打印星座名稱,然後打空白行並停止打印,你需要考慮那種可能性。

0
public static void getDailyHoroscope() throws IOException{ 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please input your zodiac sign:"); 
    String sign = sc.nextLine(); 
    String file = "C:/Users/Laptop/Downloads/horoscope.txt"; 
    sc.close(); 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     //include boolean exist sign, only because message 
     boolean existSign = false; 

     while ((line = br.readLine()) != null) { 
      if (line.equalsIgnoreCase(sign)) { 
       //skip first empty line 
       br.readLine(); 
       line = br.readLine(); 
       while (line!=null && !(line.isEmpty())) { 
        System.out.println("\n"+line); 
        line = br.readLine(); 
       } 

       existSign = true; 
       break; 
      } 
     } 
     if (!existSign) 
      System.out.println("Please input zodiac sign which exists"); 

     br.close(); 
    } catch (FileNotFoundException e) { 
     System.out.println("Cannot find" + file); 
    } 
} 

在outer while循環讀取行中,行不等於sign。 走進如果讀取一行跳過空行,塔爾閱讀有用的文本行之後。只要它到達末尾或下一行,打印該行並讀取下一行。在內部的同時,設置存在標誌爲真,並在外部中斷。

+0

這測試和工作正常。 –

+1

我無法想象op的每個段落都以'[end]。'結尾,就像我懷疑它們都有'[continue horoscope]'的內容' – zack6849

+0

請解釋它爲什麼可行。雖然代碼有很多幫助,但理解通常需要的不僅僅是這些。 – Flummox

相關問題