I have a text file with the following String:
I want to know. bye bye. I found you. I hate you. I hear you.
What I want to do is to search for a target sentence inside a file. This is the code that I use:
public String lookFor(String target, File targetDestination) throws FileNotFoundException {
Scanner scan = new Scanner(targetDestination);
scan.useDelimiter("\\. ");
while (scan.hasNext()) {
if (scan.next().compareTo(target) == 0)
return target;
}
return "Sorry,(" + target + ") cannot be found!";
}
The code works just fine when ever I try to look for any sentence such as: "I hate you" it returns "I hate you", but when I try to return the last sentence "I hear you" it says that it's not found, until I add a dot "I hear you." then it returns it.
Can anyone explain what is happening exactly? I feel that it's the delimiter, but I don't know much about regular expressions.