I would like to compare my news strings that contains a keyword from Item VoiceSearch. If some any of my News item strings contains the VoiceSearch result then say the string result.
val List<String> matches = newArrayList // you will need to import java.util.List
NewsSense.members.forEach[ news |
if(news.state.toString.contains(VoiceSearch.state.toString)) {
matches.put(news.state.toString)
]
// matches now contains all the text from the NewsSense Items that contain the String in VoiceSearch
One more stupid question. How do I call/get the [quote=“rlkoshak, post:2, topic:15760”]
matches
[/quote] results and [quote=“rlkoshak, post:2, topic:15760”]
news
[/quote] - is that a Item?
matches is a list. You can get the results one by one using matches.get(0) through matches.get(matches.size-1) or iterate through them again using matches.forEach[ match | do something].
“news” is just a variable name so I can reference the current thing from the list. In another language or pseudocode the above might looks like:
for each StringItem news in NewSense.members do:
if(news.state.toString.contains ...
The body of the for loop will execute on each and every thing in members. “news” is just a way to refer to the current thing we are iterating over. You could call it anything you want.
I recommend spending some time reading the OH wiki and OH docs because that statement makes absolutely no sense.
Let me break it down:
val lastString = matches.get(matches.size-1)
lastString.put(Info.state) // lastString is a String, it has no put method. Info.state is not a String so even if there was a put method this wouldn't work, furthermore, this would add Info.state to lastString, not set Info.state to lastString
The correct way to do this is:
val lastMatch = matches.last
Info.sendCommand(lastMatch)
say("I found " + lastMatch, " Klara")