Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching a word for a specific character at a set point (Java)
    text
    copied!<p>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. </p> <p>I'm writing this method by TDD is eclipse and so far my method is as follows:</p> <pre><code> 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 } </code></pre> <p>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.</p> <p>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. </p> <p>I'm not sure how to fix this in order for the code to run without the test failing. Thanks for your help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload