-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”)中添加的那样,代码运行直到。
我不知道如何解决这个问题,以便代码在没有测试失败的情况下运行。谢谢你的帮助。
你打电话给你的功能?什么是确切的论点? – 2012-02-09 16:01:34
定义了“lengthOfWord”的位置? – talnicolas 2012-02-09 16:01:58
我在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