在设定点搜索单词以查找特定字符(Java)
我正在写一个方法,通过一个dictionary搜索多个相同长度的单词,这些单词在一个设定点上包含相同的字母。即,所有长度为5且第二个字母为b的单词 我用TDD编写的这个方法是eclipse,到目前为止,我的方法如下:在设定点搜索单词以查找特定字符(Java),java,junit,java.util.scanner,Java,Junit,Java.util.scanner,我正在写一个方法,通过一个dictionary搜索多个相同长度的单词,这些单词在一个设定点上包含相同的字母。即,所有长度为5且第二个字母为b的单词 我用TDD编写的这个方法是eclipse,到目前为止,我的方法如下: private OpenQueue openQueue = new OpenQueue(); private boolean value; private int lengthOfWord, numberFound; private File inF
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时,它说它预期为true,但它为false
代码在while(word.charAt(crossingPoint-1)=letter)循环之前运行,但不会超过该循环,正如前面我在System.out.println(“Here”)中添加的那样,在该循环之前检查代码的运行情况
我不知道如何修复这个问题,以便代码在测试不失败的情况下运行。谢谢你的帮助 很难看到这段代码--arghh!但似乎至少有一个语法错误。我不确定你是否只是错误地将它复制到了这个问题中,否则我甚至不知道它是如何编译的。在lengthOfWord后面加上括号,使其看起来像一个无参数方法或方法调用,但似乎希望将其用作整数变量
此外,未定义infle和numberFound。您必须提供更多的上下文。您如何调用函数?确切的参数是什么?在哪里定义了
lengthOfWord
?我在JUnit测试中调用该函数,方法是(对formmatting表示歉意):private search search fiveletterword=new search(5)@Test public void findafiveletterwordthasasecondletterb(){assertEquals(“找到一个包含第二个字母B的五个字母的单词并打印出来”),true,searchfiveletterwords.examie2(2,B));}