2016-12-13 81 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

相关问题