2013-11-23 26 views
0

场景:我打算把一天的小费当做任务,我尝试了很多,并且可以做到最低级别: 我的问题是如何从当前日期的文件库中生成语句,如果语句已经显示为当前日期,不再显示,从文件中获得另一个随机语句。 PS:抱歉,我的英语不好如何从当前日期的文件库中生成语句?

import java.io.*; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Random; 
class FileRead { 
    public static void main(String args[]) 
    { 
     ArrayList<String> tip = new ArrayList<String>(); 
     Random r= new Random(); 
     int n= r.nextInt(10); 
     try{ 


      FileInputStream fstream = new FileInputStream("d:/MyPhrases.txt"); 
      // use DataInputStream to read binary NOT text 
      // DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
      String strLine; 
      //Read File Line By Line 
      while ((strLine = br.readLine()) != null) { 
      // write the content on the arraylist 
       tip.add(br.readLine()); 
      } 
      //Close the input stream 
      in.close(); 
      // Print the content on the console 
      while (tip != null) { 
       //Collections.shuffle(tip); 
       System.out.println (tip.get(n).toString()); 
       System.exit(0); 
      } 

     }catch (Exception e){ 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 
+0

你应该给一个什么样的文件看起来像一个片段。 –

+0

你的问题很不清楚你想达到什么目的。您的文件的几行将有所帮助。我们不知道你如何确定日期。文件中是否有日期? –

+0

另外我没有得到你的程序的随机部分。什么应该是随机的? –

回答

0

对于此行,

tip.add(br.readLine()); 

我相信你想

tip.add(strline); 

如果您使用的第一个,该列表将添加下一个行,而不是当前行。

编辑:

while ((strLine = br.readLine()) != null && i <= n) { 
    if (!tip.contains(strLine)) { 
     tip.add(strLine); 
     break; 
    } 
    n++; 
} 
相关问题