你好,我是想实现一个逻辑检查字符串中的Java的
1)我会得到多个消息 (例如消息: 解决方案不工作, 错过了一些东西, 留言失败, 按预期工作 )
2)我有列表中的字词(不工作,未接来电,已失败)
我需要考虑的消息只包含任何对的字E
试图像string.contains(WORD1)|| string.contains(word2)但有n个单词。 谢谢
你好,我是想实现一个逻辑检查字符串中的Java的
1)我会得到多个消息 (例如消息: 解决方案不工作, 错过了一些东西, 留言失败, 按预期工作 )
2)我有列表中的字词(不工作,未接来电,已失败)
我需要考虑的消息只包含任何对的字E
试图像string.contains(WORD1)|| string.contains(word2)但有n个单词。 谢谢
这里是你的问题完整的解决方案:
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
}
}
}
让我知道,如果你面对任何问题,而理解我的代码
你试过哪些?你的努力是什么? – XtremeBaumer
如果列表的话是有限的,我可以说去,但有字的n个 – Ftrace
使用了循环来检查 – forJ