要输入一个句子并检查它是否包含用户输入的任何单词,也打印计数

时间:2015-05-26 06:30:51

标签: java

我正在研究java中的程序,输出应该如下所示:

Input the sentence
hello how how are you
enter code here

Input the word that has to be searched
how

Output : 
the string is present and the count of the string how is : 2

I have written a program but i am not able to count the search string can anyone please help me on this and below is the code

我认为循环存在问题,我能够找到句子中的字符串,但无法计算。

        boolean contains = false;

        /*Inputting the sentence*/
        java.util.Scanner scn = new java.util.Scanner(System.in);

        System.out.println("Input the sentence");
        String s = scn.nextLine();
        String[] lstarr = s.split(" ");

        /*Inputting the string*/
        java.util.Scanner scn2 = new java.util.Scanner(System.in);

        System.out.println("Input the word to be searched");
        String s2 = scn.nextLine();
        String[] explst = s2.split(" ");

        /*searching the input word */
        if(s.contains(s2)){    
            contains = true;
            System.out.println("Input word is present : " + s2);
        }

        else{

            System.out.println("String " + s2 + "is not present");
        }

        ArrayList<String> lst = new ArrayList<String>();

        Collections.addAll(lst, lstarr);

        for(String str : lst) {

            System.out.println(str + " " + Collections.frequency(lst, str));

        }
    }

5 个答案:

答案 0 :(得分:1)

无需使用Collections.addAll

这将为您提供输入句子中所有单词的频率。

List<String> input = Arrays.asList(lstarr);
for(String str : input) {
    System.out.println(str + " " + Collections.frequency(input , str));
}

当然,如果您只想要搜索单词的频率,则需要:

System.out.println(s2 + " " + Collections.frequency(input, s2));

答案 1 :(得分:1)

请尝试以下代码:

public static void main(String[] args) {

     System.out.println("Input the sentence");
     Scanner s = new Scanner(System.in);
     String input = s.nextLine();

     System.out.println("Input the word that has to be searched");
     String word = s.nextLine();

     String str = "";
     int occurance = 0;
     for(char c : input.toCharArray()) {
         str += c;
         if(str.length() == word.length()) {
             if(str.equals(word)) {
                 occurance ++;
             }

             str = str.substring(1);
         }
     }

     if(occurance > 0)
         System.out.println("the string is present and the count of the given string is : " + occurance);
     else 
         System.out.println("The string is not present");
}

答案 2 :(得分:1)

因此,您的基本问题围绕着将数组(String s)转换为List String

的愿望。

您可以至少以三种方式向集合添加值数组...

您可以使用Arrays.asList ...

List<String> lst = Arrays.asList(lstarr);

返回一个不可变的List,它可以满足您的需求,但您也可以使用...

ArrayList<String> lst = new ArrayList<String>(Arrays.asList(lstarr));

ArrayList<String> lst = new ArrayList<String>();
lst.addAll(Arrays.asList(lstarr));

哪个会给你一个可变列表...

然后,您可以直接使用Collections.frequency来检查作品的频率......

System.out.println(s2 + " " + Collections.frequency(lst, s2));

多字搜索...(由于某种原因)

    String text = "how hello how how are you";
    String query = "how hello";

    String[] words = text.split(" ");
    List<String> wordsList = Arrays.asList(words);
    String[] matches = query.split(" ");
    for (String match : matches) {
        System.out.println(match + " occurs " + Collections.frequency(wordsList, match) + " times");
    }

根据我的输入,输出

how occurs 3 times
hello occurs 1 times

你甚至可以使用正则表达式......

for (String match : matches) {
    Pattern p = Pattern.compile(match);
    Matcher m = p.matcher(text);

    int count = 0;
    while (m.find()) {
        count++;
    }
    System.out.println(match + " occurs " + count + " times");
}

您甚至可以使用临时列表并简单地从中删除给定单词的所有匹配项并计算大小差异...

List<String> check = new ArrayList<>(25);
for (String match : matches) {
    check.addAll(wordsList);
    int startSize = check.size();
    check.removeAll(Arrays.asList(new String[]{match}));
    int endSize = check.size();
    System.out.println(match + " occurs " + (startSize - endSize) + " times");
    check.clear();
}

但我认为这就是&#34;方式&#34;超出所要求的......

答案 3 :(得分:0)

在检查输入是否存在之前,只需创建您的收藏。 在if内使用Collections.frequency(lst, s2)打印输入频率 在if / else

之后删除代码
    ...   
    ArrayList<String> lst = new ArrayList<String>();

    Collections.addAll(lst, lstarr);

    if(lst.contains(s2)){
        System.out.println("Input word is present and the count of the string "+s2+" is :" + Collections.frequency(lst, s2));
    }
    ...

答案 4 :(得分:0)

使用以下代码 -

java.util.Scanner scn = new java.util.Scanner(System.in);

        System.out.println("Input the sentence");
        String s1 = scn.nextLine();

        System.out.println("Input the word or combination-of-words to be searched");
        String s2 = scn.nextLine();

        /*searching the input word */

        int count=0;

        while(s1.contains(s2)){    
            s1=s1.substring(s1.indexOf(s2)+s2.length(), s1.length()-1);
            count++;
        }
        if(count>0){
            System.out.println("String "+s2+" exists, occures "+count +" times.");
        }else{
            System.out.println("String "+s2+" does not exists.");
        }