2017-07-05 48 views
-4
提供給任意一個字詞

你好,我是想實現一個邏輯檢查字符串中的Java的

1)我會得到多個消息 (例如消息: 解決方案不工作, 錯過了一些東西, 留言失敗, 按預期工作 )

2)我有列表中的字詞(不工作,未接來電,已失敗)

我需要考慮的消息只包含任何對的字E

試圖像string.contains(WORD1)|| string.contains(word2)但有n個單詞。 謝謝

+2

你試過哪些?你的努力是什麼? – XtremeBaumer

+0

如果列表的話是有限的,我可以說去,但有字的n個 – Ftrace

+1

使用了循環來檢查 – forJ

回答

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 
     } 
    } 
} 

讓我知道,如果你面對任何問題,而理解我的代碼

+0

謝謝你能分享一些怎麼樣,我可以調用這個列表的詳細信息的話整個列表中的平等來自財產文件的單詞。我可以用逗號分隔成一個鍵嗎? – Ftrace

+0

肯定。只要告訴我,從那裏您的郵件來了 – sForSujit

+0

這是基於請求時,它就會發出響應的第三方工具之一。在未來幾年更多的消息和文字可以添加... – Ftrace