By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

Needing Serious help

P: 4
Hey, I need to make a program that will
- read in a text file
- count the number of words in the file
- count the length frequency of words

If anyone has any code or knows of anywhere i can find code it would be most appreciated. I have tried to make the program but i dnt know where to startor how i should code.

Thanks

Arch
Dec 27 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Hey, I need to make a program that will
- read in a text file
- count the number of words in the file
- count the length frequency of words

If anyone has any code or knows of anywhere i can find code it would be most appreciated. I have tried to make the program but i dnt know where to startor how i should code.

Thanks

Arch
Post the code you've written so far and we will be able to help you. You may need to make use of a combination of BufferedReader and FileReader classes from java.io.
Dec 28 '06 #2

P: 4
Post the code you've written so far and we will be able to help you. You may need to make use of a combination of BufferedReader and FileReader classes from java.io.
Hi thanks for this, i have come up with this code so far buit i dont think its any good.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class App
  5. {
  6.             //Create BufferedReader class instance
  7.             public static InputStreamReader input = new InputStreamReader(System.in);
  8.             public static BufferedReader keyboardInput = new BufferedReader (input);
  9.  
  10.  
  11.  
  12.     public static void main(String[] args) throws IOException
  13.     {
  14.  
  15.         System.out.println("Enter the name of the file you wish to read:  ");
  16.  
  17.         FileReader file = new FileReader();
  18.         BufferedReader MyFile = new BufferedReader(file);
  19.  
  20. StringTokenizer TokenizeMe;
  21. int numTokens = 0;
  22. int numWords = 0;
  23. int numChar= 0;
  24.  
  25. TokenizeMe = new StringTokenizer(Myfile.readLine());
  26. numTokens = TokenizeMe.countTokens();
  27.  
  28. while (numTokens != 0)
  29. {
  30.     for (int WordsinLine=1;
  31.             WordsinLine<=NumberofTokens;WordsinLine++);
  32.     {
  33.         System.out.print(TokenizeMe.nextToken()+" ");
  34.     }
  35.  
  36.         System.out.println();
  37.  
  38.         numWords += NumberofTokens;
  39.  
  40.         TokenizeMe = new
  41.                 StringTokenizer(MyFile.readLine());
  42. }
  43.  
  44. System.out.println("\nThere are " + NumberofWords + " words in the text");
  45.  
  46. MyFile.close();
  47. }
  48.         }
  49.         }
  50.  
THis is my code i made it in a matter of minutes, so alot of it is crap hope you can find something from it.
Dec 28 '06 #3

10K+
P: 13,264
Hi thanks for this, i have come up with this code so far buit i dont think its any good.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class App
  5. {
  6.             //Create BufferedReader class instance
  7.             public static InputStreamReader input = new InputStreamReader(System.in);
  8.             public static BufferedReader keyboardInput = new BufferedReader (input);
  9.  
  10.  
  11.  
  12.     public static void main(String[] args) throws IOException
  13.     {
  14.  
  15.         System.out.println("Enter the name of the file you wish to read: ");
  16.  
  17.         FileReader file = new FileReader();
  18.         BufferedReader MyFile = new BufferedReader(file);
  19.  
  20. StringTokenizer TokenizeMe;
  21. int numTokens = 0;
  22. int numWords = 0;
  23. int numChar= 0;
  24.  
  25. TokenizeMe = new StringTokenizer(Myfile.readLine());
  26. numTokens = TokenizeMe.countTokens();
  27.  
  28. while (numTokens != 0)
  29. {
  30.     for (int WordsinLine=1;
  31.             WordsinLine<=NumberofTokens;WordsinLine++);
  32.     {
  33.         System.out.print(TokenizeMe.nextToken()+" ");
  34.     }
  35.  
  36.         System.out.println();
  37.  
  38.         numWords += NumberofTokens;
  39.  
  40.         TokenizeMe = new
  41.                 StringTokenizer(MyFile.readLine());
  42. }
  43.  
  44. System.out.println("\nThere are " + NumberofWords + " words in the text");
  45.  
  46. MyFile.close();
  47. }
  48.         }
  49.         }
  50.  
THis is my code i made it in a matter of minutes, so alot of it is crap hope you can find something from it.
Does this one work for you?

Expand|Select|Wrap|Line Numbers
  1.   import java.io.*;
  2. import java.util.*; 
  3. public class App {
  4.  //Create BufferedReader class instance
  5.  public static InputStreamReader input = new InputStreamReader(System.in);
  6.  public static BufferedReader keyboardInput = new BufferedReader (input);
  7.  public static void main(String[] args) {
  8.   int[] lengths = new int[100]; //Runtime error if the file contains a word of more than 100 characters
  9.   int longestWord = 0;
  10.         for(int i = 0; i < lengths.length; i++) {
  11.    lengths[i] = 0;
  12.   }
  13.   try {
  14.    System.out.println("Enter the name of the file you wish to read:  ");
  15.    String fileName = keyboardInput.readLine();
  16.    FileReader file = new FileReader(new File(fileName));
  17.    BufferedReader myFile = new BufferedReader(file);
  18.    int numTokens = 0;
  19.    int numWords = 0;
  20.    int numChar= 0;
  21.    String line;
  22.    while ((line = myFile.readLine()) != null ) {
  23.     String[] words = line.split(" ");
  24.     numWords = numWords + words.length;
  25.     for (int i = 0; i < words.length;i++) {
  26.      numChar = numChar + words[i].length();
  27.      if(words[i].length() > longestWord) {
  28.       longestWord = words[i].length();
  29.      }
  30.      lengths[words[i].length()] = lengths[words[i].length()] + 1;
  31.     }
  32.    }
  33.    System.out.println("\nThere are " + numWords + " words in the text file");
  34.    System.out.println("\nThere are " + numChar + " characters in the text file");
  35.    System.out.println("\nThere length frequency of the words is as follows:");
  36.    System.out.println("\nLength   :  Frequency ");
  37.    for(int i = 0; i <= longestWord; i++) { //Print frequencies only up to the longest word
  38.     System.out.println(i + "     :"+lengths[i]);
  39.    }
  40.    myFile.close();
  41.   }
  42.   catch(IOException iO) {
  43.    iO.printStackTrace();
  44.   }
  45.  }
  46. }
  47.  
Dec 29 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.