I am trying to find the number of occurrences of each word in string.
For example Sentence=" My home, is my home"
. The answer is "my"=2; "home"=2; "is"=1
Here is my code:
int count;
count = 0;
String text[] = new String[100];
String Temp[] = new String[100];
text = Text.getText().split("\\W");
Temp = Text.getText().split("\\W");
for (int j = 0; j < text.length; j++) {
for (int i = 0; i < text.length; i++) {
if (text[j].equals(Temp[i])) {
count += 1;
System.out.println(text[i] + "-" + count);
}
}
}
Input: me.me.me.me
Output:
me-1
me-2
me-3
me-4
me-5
me-6
me-7
me-8
me-9
me-10
me-11
me-12
me-13
me-14
me-15
me-16