Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Networking TechnologyRSS Feeds

Word list program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file

Posted By: Hannah Campbell     Category: Java     Views: 6137

The wordlist program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file. Improve the program so that it also keeps track of the number of times that each word occurs in the file. Write two lists to the output file. The first list contains the words in alphabetical order.The number of times that the word occurred in the file should be listed along with the word. Then write a second list to the output file in which the words are sorted according to the number of times that they occurredin the files. The word that occurred most often should be listed first.

Code for Word list program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file in Java

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);
    
      }
      
      }   
}
  
Share: 



Hannah Campbell
Hannah Campbell author of Word list program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file is from Toronto, Canada.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!