To enter sentence input and check if it contains any word entered by the user, also print the counter
I am working on a program in java, the output should look something like this:
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 loop and also I can find the line present in the sentence but I can’t 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));
}
}
No need to use Collections.addAll
:
This will give you the frequency of all 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 word you are looking for, you need:
System.out.println(s2 + " " + Collections.frequency(input, s2));
Try using 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");
}
So your main problem is with wanting to convert array ( String
s) to List
from String
s
You can add an array of values to a collection in at least three ways ...
You can use Arrays.asList
...
List<String> lst = Arrays.asList(lstarr);
which returns not mutable List
, which will suit your needs, but you can also use ...
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 modified list ...
Then you can check the frequency of operation simply by using Collections.frequency
directly ...
System.out.println(s2 + " " + Collections.frequency(lst, s2));
Searching multiple words ... (for whatever 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 can even use regex ...
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 can even use a temporary list and simply remove all occurrences of a 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 this is a "way" above what was asked ...
Just create your collection before checking for input. Internally, if
print the input frequency with Collections.frequency(lst, s2)
Get rid of the code after 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));
}
...
use the 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.");
}