Scanning document and finding certain criteria.
Currently have an assignment where we show an open prompt, have the user select a text file, prompt for a word to search for, then display a dialog saying how many times that word was found among other things. One of the things to search for are the words that come closest before and after the target word. So, for example, if the target is the word "the", the alphabetically previous and next words might be "that" and "there".
So heres the start of the code for that portion. I have everything else working, just gotta figure out how to find these words..
String charequals = ""; //hastily named variable to keep track of common words...
Scanner scanner = new Scanner(text); //text is the File method name to keep ahold of the text file
word = scanner.next().toLowerCase();
while(scanner.next().toLowerCase().charAt(0)==userTarget.charAt(0)) //userTarget is the users typed in word.
//This is where I'm stuck. not sure how to keep comparing to find the right word. Maybe an easier method is available?
A friend figured out how to get it to find the previous word, but the next word part wont work using a similar code...
if (word.compareTo(userTarget) < 0 && word.compareTo(prev) > 0) //this works fine for the word alphabetically previous
prev = word;
if(word.compareTo(userTarget) > 0 && word.compareTo(next) < 0) //reversing the signs, this does NOT work... why?
next = word;