I'm trying to find the word alphabetically following a user inputted word in a text document... Why is this finding the word at the end of the alphabet contained in the document rather than right after the user word? (i.e. "zulu")
In my file, it keeps listing the word "zulu", which i assume would be the last word of the alphabet in this particular document. I've tried switching and flipping signs and variables, but this gets me closest to what i need..
String next = null; //variable to hold the value we're looking for.
word = scanner.next(); //hold the scanners next object
if(next != null && next.compareTo(word) < 0 && word.compareTo(next) > 0 || next == null && word.compareTo(userTarget) < 0)
next = word;
You'll need some sort of loop construct (like "while") to work through the words in the text document checking each against both the userTarget (to see if its after the target word and hence a possible candidate) and against the best candidate you have found so far (to see if its before that word in which case you have a better candidate).