import java.io.*;
class cntwords
{
publicstaticvoid main(String args[]) throws IOException
{
try
{
FileReader fr = new FileReader("data.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null)
countwords(str);
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found");
}
catch(IOException e)
{
System.out.println("Exception : " + e);
}
}
staticvoid countwords(String st)
{
int len,i,totwords=1,j=-1,k=0,freqarr[],temp;
int cnt=0,flag=0;
len = st.length();
char ch[];
String starr[],st1[];
for(i=0;i<len-1;i++)
{
if(st.charAt(i) != ' ' && st.charAt(i+1) == ' ')
totwords++;
}
starr = new String[totwords+1];
freqarr = newint[totwords];
st1 = new String[totwords];
for(i=0;i<len-1;i++)
{
if(st.charAt(i) != ' ' && st.charAt(i+1) == ' ')
{
ch = newchar[(i+1) - j];
st.getChars(j+1,i+1,ch,0);
starr[k++] = new String(ch);
j = i+1;
}
}
ch = newchar[(i+1) - j];
st.getChars(j+1,i+1,ch,0);
starr[k] = new String(ch);
k=0;
for(i=0;i<totwords;i++)
{
temp = 1;
flag = 0;
for(j=i+1;j<=totwords;j++)
{
if(starr[i].equals(starr[j]))
temp++;
}
freqarr[k] = temp;
for(cnt=0;cnt<k;cnt++)
{
if(starr[i].equals(st1[cnt]))
flag = 1;
}
if(flag == 0)
st1[k++] = starr[i];
}
for(i=0;i<k;i++)
System.out.println(st1[i] + "has count : " + freqarr[i]);
writedata(st1,freqarr,k);
}
staticvoid writedata(String arr[],int intarr[],int len)
{
try
{
FileOutputStream fos = new FileOutputStream("res.txt");
FileOutputStream fos1 = new FileOutputStream("res1.txt");
DataOutputStream dos = new DataOutputStream(fos);
int i,j,val;
String temp;
for(i=0;i<len;i++)
{
dos.writeChars(arr[i]);
dos.writeChars(Integer.toString(intarr[i]));
dos.writeChar(' ');
}
fos.close();
dos = new DataOutputStream(fos1);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(intarr[i] < intarr[j])
{
val = intarr[j];
intarr[j] = intarr[i];
intarr[i] = val;
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(i=0;i<len;i++)
{
dos.writeChars(arr[i]);
dos.writeChars(Integer.toString(intarr[i]));
dos.writeChar(' ');
}
fos1.close();
}
catch(IOException e)
{
System.out.println("Exception : " + e);
}
}
}