/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
Set<String> result = new HashSet<>()
StringString[] wordsInSentence = sentence.split(" ");
for( String word : wordsInSentence )
{
for( String word2 : words )
{
if ( word.equals(word2) )
{
result.add( word );
break;
}
}
}
}
It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.
/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
Set<String> result = new HashSet<>()
StringString[] wordsInSentence = sentence.split(" ");
for( String word : wordsInSentence )
{
if ( words.contains(word) )
result.add( word );
}
}