您好我正在尝试实现一个逻辑
1)我会收到多条消息 (例如: 解决方案不起作用 错过了什么, 消息失败, 按预期工作 )
2)我有列表单词(不工作,错过,失败)
我需要考虑仅包含任何一个单词的消息
尝试过string.contains(word1)|| string.contains(word2)但是有n个单词。 谢谢
答案 0 :(得分:0)
以下是您问题的完整解决方案:
package com.sujit;
import java.util.ArrayList;
import java.util.List;
public class CompareMessages{
public static void main(String[] args) {
//Assume the messages as per you said
List<String> messages = new ArrayList<>();
messages.add("Solution not working");
messages.add("Message Failed");
messages.add("Missed something");
messages.add("This message will be ignored");
//Your standard list of words
List<String> list = new ArrayList<>();
list.add("Not working");
list.add("missed");
list.add("failed");
Integer count=0;
String msg = null; //this will store which message will be ignored
for (String string : messages) {
for(String standard : list) {
if(string.toLowerCase().contains(standard.toLowerCase())){ //check if message if present in your standard list
count = 1; //if it is present, Count becomes 1
if(count==1){
msg = null;
break; // we can break the loop for current message to be checked with standard list if it is present
}
}
else{
count=0; // if the message is not your standard, then count becomes 0
msg = string;
}
}
}
if(count==0){
System.out.println(msg); //finally you can get which messages is wrong according to your standard list
}
}
}
如果您在理解我的代码时遇到任何问题,请告诉我