java 要输入一个句子并检查它是否包含用户输入的任何单词,还要打印计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30451041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To take input a sentence and check if it contains any word input by the user, also print the count
提问by Ashu123
I am working on a program in java the output should be something like below :
我正在用 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
I think there is a problem with the looping as well i am able to find the string present in the sentence but not able to count.
我认为循环也存在问题,我能够找到句子中存在的字符串但无法计数。
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));
}
}
采纳答案by Soumitri Pattnaik
Try the following code :
试试下面的代码:
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");
}
回答by Eran
There's no need to use Collections.addAll
:
没有必要使用Collections.addAll
:
This will give you the frequency of all the words in the input sentence.
这将为您提供输入句子中所有单词的频率。
List<String> input = Arrays.asList(lstarr);
for(String str : input) {
System.out.println(str + " " + Collections.frequency(input , str));
}
Of course, if you only want the frequency of the searched word, you need :
当然,如果你只想要搜索词的频率,你需要:
System.out.println(s2 + " " + Collections.frequency(input, s2));
回答by MadProgrammer
So, your basic problem revoles around the desire to convert an array (of String
s) to a List
of String
s
因此,围绕着渴望你的基本问题revoles到一个数组(转换的String
S)连接到List
的String
小号
You can add an array of values to a collection in at least three ways...
您可以通过至少三种方式将一组值添加到集合中...
You could use Arrays.asList
...
你可以用Arrays.asList
...
List<String> lst = Arrays.asList(lstarr);
which returns a non-mutable List
, which would suit your needs, but you can also use...
它返回一个 non-mutable List
,这将满足您的需要,但您也可以使用...
ArrayList<String> lst = new ArrayList<String>(Arrays.asList(lstarr));
or
或者
ArrayList<String> lst = new ArrayList<String>();
lst.addAll(Arrays.asList(lstarr));
Which will give you a mutable list...
这会给你一个可变列表......
You can then check for the frequency of the work simply by using Collections.frequency
directly...
然后,您只需Collections.frequency
直接使用...
System.out.println(s2 + " " + Collections.frequency(lst, s2));
Multiple word search...(for some reason)
多词搜索...(出于某种原因)
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");
}
Which, based on my input, outputs
其中,根据我的输入,输出
how occurs 3 times
hello occurs 1 times
You could even use a regular expression...
您甚至可以使用正则表达式...
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");
}
You could even use a temporary list and simple remove all the occurrences of the given word from it and calculate the difference in size...
您甚至可以使用临时列表并简单地从中删除所有出现的给定单词并计算大小差异...
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();
}
But I think that goes "way" beyond what is been asked...
但我认为这超出了所要求的“方式”......
回答by Nitin Dandriyal
Just create your collection before you check the existence of input.
Inside if
print the frequency of input using Collections.frequency(lst, s2)
Get rid of the code after if
/else
在检查输入是否存在之前,只需创建您的集合。里面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));
}
...
回答by Nitin Dandriyal
use following code-
使用以下代码-
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.");
}