Hi,
That sentence was just an Example, i want to calculate the number of words which are repeating in the sentence, where sentence is any sentence that user enter.
Plz explain me how to do that. I used your program. bt not getting how to pass the array instead of p there.
i did like this :
--------------------------------------------------------------------
import java.util.*;
import java.io.*;
import java.*;
public class CountWords {
public static void main(
String[] args) throws IOException
{
InputStreamReader data = new InputStreamReader(System.in);
BufferedReader strData = new BufferedReader(data);
System.out.print("Enter Data :: ");
String strMainData = " ";
strMainData = strData.readLine();
System.out.print("Entered Data :: " + strMainData);
String strParts[];
strParts = strMainData.split(" ");
//String s = "My first name is sheetal, My last name is kaul.";
//String p = "My";
for(int i = 0; i < strParts.length; i++)
{
System.out.println("'" + strParts[i] + "' occures " + CountWords.count(strMainData, strParts[i]) + " times in '" + strMainData + "'");
}
}
public static int count(String s, String p[])
{
String sTemp = s;
int counter = 0;
while (sTemp.length() > 0)
{
for(int j = 0; j < sTemp.length; j++) {
int index = sTemp.indexOf(
pattern[j]);
if (index == -1)
break;
sTemp = sTemp.substring(index + pattern[j].length(), sTemp.length());
counter++;
}
}
return counter;
}
}
--------------------------------------------------------------------
but it is giving error. Plz explain me how to do thia program.
Thanxs
Sheetal