你好,我是想實現一個邏輯檢查字符串中的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