2012-02-09 52 views
-1

我正在寫一個方法來搜索一個dicitionary以查找包含相同字母的設定點的多個單詞。即所有長度爲5的單詞都以b爲第二個字母。在設定點搜索特定字符的單詞(Java)

我寫的TDD這種方法是日食,到目前爲止我的方法如下:

private OpenQueue openQueue = new OpenQueue(); 
    private boolean value; 
    private int lengthOfWord, numberFound; 
    private File inFile = new File("src/src/WordList"); //This is a text file 

    public Search(int length) { 
     this.lengthOfWord = length; 
    } 

    public boolean examine2(int crossingPoint, char letter) { 
     try { 
     Scanner input = new Scanner(inFile);   
     while (input.hasNextLine()) { //while there are words left to be read 
      String word = input.nextLine();  
      if(word.length() == lengthOfWord) { //if the word is of the right length 
       while(word.charAt(crossingPoint-1) == letter){ 
       numberFound = numberFound + 1; //number of solutions is increased by one 
       openQueue.add(word);//word is added to the open queue 
       value = true; //value is true when at least one solution has been found 
       } 
      } 
     } 
     } catch (FileNotFoundException e) { 
      System.out.println("They File was not Found"); 
      e.printStackTrace(); 
     } 
     System.out.println(numberFound); //returns number of words found 
     return value; //should return true if there is at least one word 
    } 

對於我的測試,我試圖找到具有第二個字母B和有所有五個字母的單詞我已經手動檢查過幾個詞。但是,當我運行JUnit時,它說它預期是真的,但它是錯誤的。

代碼運行到while(word.charAt(crossingPoint-1)==字母)循環,但不像之前在System.out.println(「Here」)中添加的那樣,代碼運行直到。

我不知道如何解決這個問題,以便代碼在沒有測試失敗的情況下運行。謝謝你的幫助。

+0

你打電話給你的功能?什麼是確切的論點? – 2012-02-09 16:01:34

+1

定義了「lengthOfWord」的位置? – talnicolas 2012-02-09 16:01:58

+0

我在JUnit測試中調用函數(關於formmatting的道歉): private search searchFiveLetterWord = new Search(5); (「Find an five letter word that have a second letter B and out out」,true,searchFiveLetterWord.examine2(2,b));}};}};}}; \t } – user977100 2012-02-09 17:39:50

回答

1

很難看這個代碼 - arghh!但似乎至少有一個語法錯誤。我不確定你是否將它錯誤地複製到這個問題中,否則我甚至不知道它是如何編譯的。你在longOfWord後面加上括號,這使得它看起來像一個無參數的方法或方法調用,但你似乎想把它用作一個整數變量。

另外inFile和numberFound似乎沒有被定義。你將不得不提供更多的上下文。