2011-10-25 79 views
0

我有以下文本fil,我在想如何解析它並搜索單元和引擎單詞,我想要的是打印出包含單元格,引擎的方法名稱話。以下是txt文件不要將其視爲java代碼,因爲我已將其移至txt文件以供解析。解析txt文件並搜索特定單詞

@Test 
    public void testGetMonsters() { 
     Cell cell11 = aBoard.getCell(1, 1); 
     theEngine = new Engine(theGame); 
    } 

    @Test 
    public void testDxDyPossibleMove() { 
     Cell cell11 = aBoard.getCell(1, 1); 
    } 

解析所需的輸出看起來像:

testGetMonsters class contains Cell and Engine words 
testDxDyPossibleMove class contains Cell word 
+0

這是令人困惑的。你能澄清你的問題和問題嗎? – Akron

+1

與第一次問你有什麼不同? –

+0

@Dave你沒有回答這兩個問題,所以謝謝你的意見,我試圖在這裏簡化問題 –

回答

0

Tadaaaa!

package textsearch; 

import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.Reader; 
import java.net.URL; 
import java.text.ParsePosition; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

public class TextSearch { 

    public static void main(final String[] args) throws Exception { 

     final URL testFileURL = Thread.currentThread().getContextClassLoader().getResource("textsearch/test.txt"); 
     final File input = new File(testFileURL.toURI()); 
     final FileReader reader = new FileReader(input); 

     final SearchString search1 = new SearchString("Cell"); 
     final SearchString search2 = new SearchString("Engine"); 

     final List<SearchString> searches = new ArrayList<SearchString>(); 
     searches.add(search1); 
     searches.add(search2); 

     final Search search = new Search(searches); 

     try { 
      search.search(reader); 
     } finally { 
      reader.close(); 
     } 

     if(search.hasPositives()) { 
      System.out.print("File " + input.getName() + " contains the words "); 
      search.printPositives(); 
      System.out.println(""); 
     } else { 
      System.out.println("File " + input.getName() + " didn't contain any of the search terms."); 
     } 

    } 

    private static class SearchString { 

     final String searchString; 
     final ParsePosition pos; 

     SearchString(final String searchString) { 

      if(searchString == null || searchString.isEmpty()) 
       throw new IllegalArgumentException("I can't search for nothing!"); 

      this.searchString = searchString; 
      pos = new ParsePosition(0); 

     } 

     boolean checkNextChar(final char c) { 

      if(searchString.charAt(pos.getIndex()) == c) { 
       pos.setIndex(pos.getIndex() + 1); 
       if(pos.getIndex() >= searchString.length()) { 
        pos.setIndex(0); 
        return true; 
       } 
       return false; 
      } else { 
       pos.setIndex(0); 
       return false; 
      } 

     } 

     String getString() { 

      return searchString; 

     } 

    } 

    private static class Search { 

     private final List<SearchString> searches; 
     private final List<SearchString> positives; 

     Search(final List<SearchString> searches) { 

      this.searches = searches; 
      positives = new ArrayList<SearchString>(); 

     } 

     void search(final Reader reader) throws IOException { 

      int current; 

      while((current = reader.read()) != -1 && !searches.isEmpty()) { 

       char c = (char)current; 

       for(final Iterator<SearchString> it = searches.iterator(); it.hasNext();) { 
        final SearchString searchString = it.next(); 
        final boolean matches = searchString.checkNextChar(c); 
        if(matches) { 
         positives.add(searchString); 
         it.remove(); 
        } 
       } 

      } 

     } 

     boolean hasPositives() { 

      return !positives.isEmpty(); 

     } 

     void printPositives() { 

      for(final Iterator<SearchString> it = positives.iterator(); it.hasNext();) { 
       final SearchString searchString = it.next(); 
       System.out.print(searchString.getString()); 
       if(it.hasNext()) 
        System.out.print(", "); 
      } 

     } 

    } 

} 

好吧,它不是100%可靠的,但它是一個開始。

編輯:或者您可以使用掃描儀一樣KayKay建議,哪來的樂趣在於:d

+0

當我添加了txt文件的最終URL的資源testFileURL = Thread.currentThread()。getContextClassLoader()。getResource(「C:/Desktop/test.txt」);它給了我這個錯誤 - >異常在線程「main」java.lang.NullPointerException \t at textsearch.TextSearch.main(TextSearch.java:18) –

+0

是的,該事情假定text.txt文件是在文本搜索包,在班級旁邊。使用'File(「C:/Desktop/test.txt」)'就足夠了。我沒有干擾乾淨的代碼,SearchString和Search類都很重要。 –

+0

哦對不起,我不熟悉這些東西,你是真棒,但問題是你在整個文件內搜索我需要的是指定這些單詞在哪裏(在哪個類),如下所示:testGetMonsters類包含單元格和引擎詞 testDxDyPossibleMove類包含單元詞 –

1

你可能想使用正則表達式和Java的模式匹配設施。以Regular Expressions and the Java Programming Language爲例。

簡單的例子:

Pattern cellPattern = Pattern.compile("Cell"); 
    while (fileReader.ready()) { 
     String inputLine = fileReader.readLine(); 
     Matcher cellMatcher = cellPattern.matcher(inputLine); 
     if(cellMatcher.lookingAt()) { 
      //This line contains the word "Cell" 

確定是否/什麼課你是另外一個問題......你需要爲一個「詞法分析器」。 JavaCC是一個很好的起點。

0

雖然我不確定我的理解您的問題,但這裏有一些提示。

要獲得班級名稱,請使用obj.getClass().getName()。 要使用str.contains(「Cell」)搜索字符串或使用正則表達式。看看類java.util.Pattern

1

我沒有看到你的鏈接代碼和問題,但之間:

String text = new Scanner(yourFile).useDelimiter("\\Z").next(); 
String wordsToLookFor = Arrays.asList("cell", "engine"); 

List<String> wordsContained = new ArrayList<String>(); 

for(String word : wordsToLookFor){ 
    if(text.contains(word)) { 
    wordsContained.add(word); 
    } 
} 

System.out.println(yourFile.getName() + " contains " + wordsContained);