在设定点搜索单词中的特定字符 (Java)
我正在编写一种方法来搜索字典,以查找在设定点包含相同字母的多个相同长度的单词。即所有长度为 5 且第二个字母为 b 的单词。
我正在通过 TDD is eclipse 编写这个方法,到目前为止我的方法如下:
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") 中添加的那样,以检查代码是否运行到。
我不知道如何解决这个问题,以便代码能够在测试失败的情况下运行。感谢您的帮助。
I am writing a method to search through a dicitionary to find multiple words of the same length that contain the same letter at a set point. I.e. All words of length 5 that have b as their second letter.
I'm writing this method by TDD is eclipse and so far my method is as follows:
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
}
For my test I trying to find all five letter words that have a second letter b and there are several words that fit this as I've checked manually. However when I run JUnit it says that it expected true but it was false.
The code runs up to but not past the while(word.charAt(crossingPoint-1) == letter) loop, as previously I added in System.out.println("Here") before this loop to check were the code runs until.
I'm not sure how to fix this in order for the code to run without the test failing. Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码很难看——啊啊!但似乎至少存在一个语法错误。我不确定您是否错误地将其复制到这个问题中,否则我什至不知道它是如何编译的。您在 lengthOfWord 后面加上括号,这使它看起来像无参数方法或方法调用,但您似乎想将其用作整数变量。
另外 inFile 和 numberFound 似乎没有定义。您将必须提供更多背景信息。
It's hard to look at this code -- arghh! But there appear to be at least one syntax error. I'm not sure whether you just copied it into this question incorrectly, otherwise I don't even see how it can compile. You put parentheses after lengthOfWord which makes it look like a no-argument method or method call, but you appear to want to use it as an integer variable.
Also inFile and numberFound do not appear to be defined. You will have to provide more context.