I want to search for a string using StringTokenizer but the string I'm looking for has a delimiter in it - Java

StackOverflow https://stackoverflow.com/questions/8813779

  •  26-10-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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";
}

OTHER TIPS

Tokenize by whitespace, then trim by the ' character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top