3

I have an external file named quotes.txt and I'll show you some contents of the file:

1 Everybody's always telling me one thing and out the other.
2 I love criticism just so long as it's unqualified praise.
3 The difference between 'involvement' and 'commitment' is like an eggs-and-ham 
  breakfast: the chicken was 'involved' - the pig was 'committed'.

I used this: StringTokenizer str = new StringTokenizer(line, " .'");

This is the code for the searching:

String line = "";
boolean wordFound = false;

while((line = bufRead.readLine()) != null) {
    while(str.hasMoreTokens()) {
       String next = str.nextToken();
       if(next.equalsIgnoreCase(targetWord) {
            wordFound = true;
            output = line;
            break;
       }
    }

    if(wordFound) break;
    else output = "Quote not found";
}

Now, I want to search for strings "Everybody's" and "it's" in line 1 and 2 but it won't work since the apostrophe is one of the delimiters. If I remove that delimiter, then I won't be able to search for "involvement", "commitment", "involved" and "committed" in line 3.

What suitable code can I do with this problem? Please help and thanks.

2 Answers2

3

I would suggest using regular expressions (the Pattern class) rather than StringTokenizer for this. For example:

final Pattern targetWordPattern =
    Pattern.compile("\\b" + Pattern.quote(targetWord) + "\\b",
                    Pattern.CASE_INSENSITIVE);

String line = "";
boolean wordFound = false;

while((line = bufRead.readLine()) != null) {
    if(targetWordPattern.matcher(line).find()) {
        wordFound = true;
        break;
    }
    else
        output = "Quote not found";
}
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thanks for this. But does this automatically ignore the case? – user1141418 Jan 11 '12 at 03:43
  • @user1141418: You're welcome! And -- do you see the `Pattern.CASE_INSENSITIVE` flag in the call to `Pattern.compile`? That's what tells it to ignore the case (or in regex terms, to perform a "case-insensitive" match). – ruakh Jan 11 '12 at 03:49
  • hi, I'm still having a problem with my program. Can I ask your help by sending me your e-mail so that I can show you the whole code? Please and thanks. – user1141418 Jan 11 '12 at 16:51
  • @user1141418: I'm not going to post my e-mail address on StackOverflow, but if you post yours, I'll e-mail you. – ruakh Jan 11 '12 at 18:50
1

Tokenize by whitespace, then trim by the ' character.

Perception
  • 79,279
  • 19
  • 185
  • 195