2013-05-30 32 views
0

我有一個文件,其中包含由\ t分隔的不同推文的信息。該信息包含用戶,語言和文本字段。逐行讀取文件並存儲到多維數組或陣列表中

我需要做的是將關於推文的信息存儲在ArrayList或數組中,以便每個單詞都是分開的,這樣我就可以遍歷它們並對它們進行比較。

這裏是文件

@GracieWhitton en RT @GracieWhitton: I need 16 more followers to 2500. I know  you are out there!! Come on folks. :) 
@SHARPErThnYu en RT @SHARPErThnYu: Stop texting me. Our relationship is non existent 
@BraandiiSongz fr RT @BraandiiSongz: Le 1er rdv chui tj timide ac une grosse boule au ventre apr c autre chose 
@BeyTomce en @BeyTomce Saturday ??? 
@VivoPorVoceLuaB pt @VivoPorVoceLuaB Segui,Sdv amore 
@JamelTaylour en "@str8BappN: @JamelTaylour That go bruh"right on bro 
@eluniweb es RT @eluniweb: #UCAB mañana martes 16 de abril hay clases http://t.co/ZeUzGBM7MI 
@MariAleAguirre es RT @MariAleAguirre: Que CINISMO el de El Ciudadano en GLOBOTERROR. Esta diciendo que VTV fue el que llamo a la violencia en Altamira... ... 

這裏是我的代碼的例子,我不知道爲什麼它doesen't工作,因爲我想要的。我不知道如何迭代數據。

public Tweets() throws FileNotFoundException { 

    Scanner in=new Scanner(new File("./twitter/data.txt")); 
    ArrayList<ArrayList<String>> comments= new ArrayList<ArrayList<String>>(); 

    while(in.hasNext()){ 
     String line=in.nextLine(); 
     String[] data=line.split("\t",-1); 

     ArrayList<String> words = new ArrayList<String>(); 
     words.add(data[0]); 
     words.add(data[1]); 
     String[] w=data[2].split(" ",-1); 

     for(int i=0;i<w.length-1;i++) 
     { 
      words.add(w[i]); 
     } 

     comments.add(words); 

    } 
    in.close(); 
} 

回答

0

我想你需要Scanner#hasNextLine

while(in.hasNextLine()){ 

同時請檢查DELIM "\t"

+0

已經檢查過。同樣的問題:/ –

+0

嘗試'String [] data = line.split(「\\ t」, - 1);' – LiverpoolFTW

0

最後我做到了。我使用ArrayList的ArrayList

我會按照我解決它的方式離開這裏。也許它對某人有用。

public Tweets() throws Exception { 


    Scanner in=new Scanner(new File("./twitter/data.txt")); 
    ArrayList<ArrayList<String>> comments= new ArrayList<ArrayList<String>>(); 
    ArrayList<String> words = new ArrayList<String>(); 
    String[] line; 
    String str; 
    String [] tweet; 
    while(in.hasNextLine()) 
    { 
     str=in.nextLine(); 
     line = str.split("\t"); 
     tweet = line[2].split(" "); 
     for(String word : tweet) 
      words.add(word); 
    } 
    comments.add(words); 
    // Printing the content of words 
    for(ArrayList<String> tt : comments) 
     for(String word : tt) 
      System.out.println(word); 
} 
相關問題