2017-07-05 46 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