Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据数组中找到的单词只返回一个句子_Java_Arrays - Fatal编程技术网

Java 根据数组中找到的单词只返回一个句子

Java 根据数组中找到的单词只返回一个句子,java,arrays,Java,Arrays,我有一个Java代码,可以根据单词数组检索句子。 文本字符串: String text = "This is a sample text. Simple yet elegant. Everyone dies. I don't care. This text is nice."; 我还有一系列单词,如下所示: String[] words = new String[] {"text", "care", "nice"}; This is a sample text. //contains the

我有一个Java代码,可以根据单词数组检索句子。 文本字符串:

String text = "This is a sample text. Simple yet elegant. Everyone dies. I don't care. This text is nice.";
我还有一系列单词,如下所示:

String[] words = new String[] {"text", "care", "nice"};
This is a sample text. //contains the word "text"
I don't care. //contains the word "care"
This text is nice. //contains the word "text" and "nice" 
现在,我需要得到数组中包含特定单词的句子。因此,要输出的句子应该包含单词“text”、“care”或“nice”。结果输出应如下所示:

String[] words = new String[] {"text", "care", "nice"};
This is a sample text. //contains the word "text"
I don't care. //contains the word "care"
This text is nice. //contains the word "text" and "nice" 
我拥有的代码是:

    public static void main(String[] args) {
    String text = "This is a sample text. Simple yet elegant. Everyone dies. I don't care. This text is nice.";
    String[] words = new String[] {"text", "care", "nice"};
    String[] parts = text.split("\\.");

    for(String w: words){
        for(String sentence: parts){
            if(sentence.contains(w)){
                System.out.println(sentence +" //contains: "+w);
            }
        }
    }   
}
但是,如果该句子包含数组中的两个单词,它将打印该句子两次。例如:

The text is nice //contains: text
The text is nice//contains: nice.
如何只打印一次句子?
谢谢

我认为最好把外环放进去。这样你就可以检查你想要的单词是否被击中,并将它们添加到本地列表中。大概是这样的:

for(String sentence: parts){

    List<String> hitList = new ArrayList<String>();

    for(String w: words){
        if(sentence.contains(w)){
            hitList.add(w);
        }
    }

    System.out.println(sentence +" //contains: "+ hitList != null ? hitList : "No match" );
}
for(字符串语句:部分){
List hitList=new ArrayList();
for(字符串w:单词){
如果(句子包含(w)){
增加(w);
}
}
System.out.println(句子+“//包含:“+hitList!=null?hitList:“不匹配”);
}

通过这种方式,您可以对您指出的案例进行分类这篇文章很好//包含单词“text”和“nice”

我认为最好将外部循环放入。这样你就可以检查你想要的单词是否被击中,并将它们添加到本地列表中。大概是这样的:

for(String sentence: parts){

    List<String> hitList = new ArrayList<String>();

    for(String w: words){
        if(sentence.contains(w)){
            hitList.add(w);
        }
    }

    System.out.println(sentence +" //contains: "+ hitList != null ? hitList : "No match" );
}
for(字符串语句:部分){
List hitList=new ArrayList();
for(字符串w:单词){
如果(句子包含(w)){
增加(w);
}
}
System.out.println(句子+“//包含:“+hitList!=null?hitList:“不匹配”);
}

通过这种方式,您可以对您指出的案例进行分类这篇文章很好//包含单词“text”和“nice”

我会使用正则表达式来处理它,比如:

String regex = ".*?(" + String.join("|", words) + ").*?";//either of one word in the sentence
for (String sentence: parts) {
    if(sentence.matches(regex)) {
        //...
    }
}

我会使用正则表达式来实现它,比如:

String regex = ".*?(" + String.join("|", words) + ").*?";//either of one word in the sentence
for (String sentence: parts) {
    if(sentence.matches(regex)) {
        //...
    }
}
Java8解决方案

    for (String sentence : parts) {

        List<String> wordsInCurrentSentence = new LinkedList<String>();

        for (String w : words) {
            if (sentence.contains(w)) {
                wordsInCurrentSentence.add(w);
            }
        }

        if (!wordsInCurrentSentence.isEmpty()) {
            String result = wordsInCurrentSentence.stream().collect(Collectors.joining(","));
            System.out.println(sentence.trim() + " //contains: " + result);
        }
    }
for(字符串语句:部分){
List words当前句子=新建链接列表();
for(字符串w:单词){
如果(句子包含(w)){
单词当前句子。添加(w);
}
}
if(!wordsCurrentSession.isEmpty()){
String result=wordsCurrentSession.stream().collect(collector.joining(“,”);
System.out.println(句子.trim()+“//包含:“+结果);
}
}
Java 8解决方案

    for (String sentence : parts) {

        List<String> wordsInCurrentSentence = new LinkedList<String>();

        for (String w : words) {
            if (sentence.contains(w)) {
                wordsInCurrentSentence.add(w);
            }
        }

        if (!wordsInCurrentSentence.isEmpty()) {
            String result = wordsInCurrentSentence.stream().collect(Collectors.joining(","));
            System.out.println(sentence.trim() + " //contains: " + result);
        }
    }
for(字符串语句:部分){
List words当前句子=新建链接列表();
for(字符串w:单词){
如果(句子包含(w)){
单词当前句子。添加(w);
}
}
if(!wordsCurrentSession.isEmpty()){
String result=wordsCurrentSession.stream().collect(collector.joining(“,”);
System.out.println(句子.trim()+“//包含:“+结果);
}
}

反转循环并添加中断。其他人已经提出了更好的方法。但只要对代码稍加修改,它就应该可以工作了。交换循环并在成功时添加中断

for(String sentence: parts){
        for(String w: words){
            if(sentence.contains(w)){
                System.out.println(sentence +" //contains: "+w);
                break;
            }
        }
    }

反转循环并添加中断。其他人已经提出了更好的方法。但只要对代码稍加修改,它就应该可以工作了。交换循环并在成功时添加中断

for(String sentence: parts){
        for(String w: words){
            if(sentence.contains(w)){
                System.out.println(sentence +" //contains: "+w);
                break;
            }
        }
    }

如果我们先检查HitReady,我们可以跳过对contains()的调用,对吗?我编辑了代码,因为我的代码不会打印出所有的命中单词,只有第一个。从你所写的,你希望能够打印出你所有的点击量。我所做的改变更适合你的情况和你的愿望。你保存列表中的所有点击,在比较之后,你只需编写/显示它们。只需在打印之前添加一个检查,确保点击列表不是空的,你就会得到我的+1。如果我们先检查HitReady,我们可以跳过对contains()的调用,对吗?我编辑了代码,因为我的代码不会打印出所有的点击词,只有第一个。从你所写的,你希望能够打印出你所有的点击量。我所做的改变更适合你的情况和你的愿望。您保存列表中的所有点击,在比较之后,您只需编写/显示它们。只需在打印之前添加一个检查,确保点击列表不为空,您就可以得到我的+1。因此,当您只打印一次句子时,是否也要打印其中包含的所有单词?是,仅检索数组中具有指定单词的句子,并列出在句子中找到的所有单词。发布我的答案。看一看。所以,当你只打印一次句子时,你希望它包含的所有单词也打印出来吗?是的,只检索数组中具有指定单词的句子,并列出在句子中找到的所有单词。发布我的答案。看一看。