2014-03-13 109 views
0

我有文件存储为“Integer-> \ t(选项卡) - >字符串 - >几个空间 - >”的数据。拆分文件只有整数和字符串字符串

我做错了吗?

我在做什么。

Trie t = new Trie(); 
    BufferedReader bReader = new BufferedReader(new FileReader(
      "H:\\100kfound.txt")); 

    String line; 
    String[] s = null; 
    while ((line = bReader.readLine()) != null) { 

     s = line.split("\t"); 

    } 
    int i; 
    for (i = 0; i < s.length; i++) { 
     System.out.println(s[i]); 
     if (!(s[i].matches("\\d+"))) { 

      t.addWord(s[i]); 
      System.out.println(s[i]); 
     } 
    } 

我可以通过调试它是正确下去,直到while循环,但在for循环,它只是存储两个字符串并打印相同的看到。

+2

你知道你用这段代码扔掉了所有的行,但最后一行吗? –

回答

1

您可能想要为表达式添加一个^ [0-9] + $,所以您只需获取完整的整数。如果没有^和$,你可能会匹配其他字符,如tt55gh会匹配。

if (!(s[i].matches("^[0-9]+$"))) { 
} 

根据上面的注释,您需要在while循环中移动for循环。

while ((line = bReader.readLine()) != null) { 

    s = line.split("\t"); 

    for (int i = 0; i < s.length; i++) { 
     System.out.println("Value "+i+": "+s[i]); 
     if (!(s[i].matches("^[0-9]+$"))) { 
      t.addWord(s[i]); 
      System.out.println("Integer "+i+": "+s[i]); 
     } 
    } 
} 
+0

没有救济乔。我有超过10000个字符串的巨大文件。它影响它。? – Hitesh