Issue
I have main string.
String main = "hi how are you? i am fine.";
and then few strings..
String string1 = "hi people what is the need................ how to ................... do the needful.................................. and you can only need the change......................................... in the fine situation................................................. to do the task................... i am alright............... are you okay?";
(may be around 100 words)
String string2 = "how to do...................i don't know the need......... what i am supposed to do......................fine i am.......... okay..";
(may be around 100 words)
String string3 = "some string again................";
(may be around 100 words)
String string4 = "some string again................";
(may be around 100 words)
Now, what I have to do is, there are 7 words in main string..
and as you can see, all those 7 words are present in string1.. 4 words are present in string2.. and continue..
So, now the percentage value for string1 is, 100%.. for string2 the percentage value is, 57.xx%.. and so on..
So, I want to get those percentage value programmatically..
What I have tried so far is,
String perc;
String[] q1 = str1.split(" ");
String[] q2 = main.split(" ");
for (String temp1: q1) {
for (String temp2: q2) {
if(temp1.equals(temp2)) {
// Some code here
}
}
}
Now I don't know where to start?
Solution
Here how it could be done:
String main = "hi how are you? i am fine.";
// Extract all the words whose length is > 1 and remove duplicates
Set<String> mainWords = new HashSet<>();
for (String s : main.split("\\W")) {
if (s.length() > 1) {
mainWords.add(s);
}
}
String string1 = "hi people what is the need................ how to ................... do the needful.................................. and you can only need the change......................................... in the fine situation................................................. to do the task................... i am alright............... are you okay?";
Set<String> mainWordsToFind = new HashSet<>(mainWords);
// Iterate over all the words and remove the word from the list to find
for (String word : string1.split("\\W")) {
if (word.length() > 1) {
mainWordsToFind.remove(word);
}
}
// Print the percent of word found
System.out.println((double) (mainWords.size() - mainWordsToFind.size()) / mainWords.size());
Output:
1.0
Answered By - Nicolas Filotto
Answer Checked By - Willingham (JavaFixing Volunteer)