0

I am using a StringTokenizer to tokenize lines of a txt file. I am searching these lines for a specific word - 'son'. How can I prevent the word 'sonnet' from being returned? See code extracts below:

String searchWord = "son";

StringTokenizer st = new StringTokenizer(text,".?!,^");

if(text.contains(searchWord)){

fw = new FileWriter("txtFiles/tokenizedSentences.txt");

bw = new BufferedWriter(fw);
bw.write(text);

Can I enter something into the StringTokenizer construct to stop from returning sonnet?

Thanks

Krupip
  • 4,404
  • 2
  • 32
  • 54
ScottAdams
  • 11
  • 2

1 Answers1

0

String Tokenizer is a legacy construct that shouldn't be used. Use String.split(regex delimiter) instead. To fix your issue you would read text like normal, but if you are only checking for son, you need to check for string equality, not just if the string contains son, so do Object.equals("son", text) to find that out. If you are looking for something more complicated than that, you will need to use Regular Expressions

Community
  • 1
  • 1
Krupip
  • 4,404
  • 2
  • 32
  • 54