Needing Serious help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Archestic
    New Member
    • Nov 2006
    • 4

    #1

    Needing Serious help

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Archestic
    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.

    Comment

    • Archestic
      New Member
      • Nov 2006
      • 4

      #3
      Originally posted by r035198x
      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.

      Code:
      import java.io.*;
      import java.util.*;
      
      public class App
      {
      			//Create BufferedReader class instance
      			public static InputStreamReader input = new InputStreamReader(System.in);
      			public static BufferedReader keyboardInput = new BufferedReader (input);
      			
      			
      			
      	public static void main(String[] args) throws IOException
      	{
      		
      		System.out.println("Enter the name of the file you wish to read:  ");
      				
      		FileReader file = new FileReader();
      		BufferedReader MyFile = new BufferedReader(file);
      
      StringTokenizer TokenizeMe;
      int numTokens = 0;
      int numWords = 0;
      int numChar= 0;
      
      TokenizeMe = new StringTokenizer(Myfile.readLine());
      numTokens = TokenizeMe.countTokens();
      
      while (numTokens != 0)
      {
      	for (int WordsinLine=1;
      			WordsinLine<=NumberofTokens;WordsinLine++);
      	{
      		System.out.print(TokenizeMe.nextToken()+" ");
      	}
      
      		System.out.println();
      		
      		numWords += NumberofTokens;
      		
      		TokenizeMe = new
      				StringTokenizer(MyFile.readLine());
      }
      
      System.out.println("\nThere are " + NumberofWords + " words in the text");
      
      MyFile.close();
      }
      		}
      		}
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Archestic
        Hi thanks for this, i have come up with this code so far buit i dont think its any good.

        Code:
        import java.io.*;
        import java.util.*;
         
        public class App
        {
        			//Create BufferedReader class instance
        			public static InputStreamReader input = new InputStreamReader(System.in);
        			public static BufferedReader keyboardInput = new BufferedReader (input);
         
         
         
        	public static void main(String[] args) throws IOException
        	{
         
        		System.out.println("Enter the name of the file you wish to read: ");
         
        		FileReader file = new FileReader();
        		BufferedReader MyFile = new BufferedReader(file);
         
        StringTokenizer TokenizeMe;
        int numTokens = 0;
        int numWords = 0;
        int numChar= 0;
         
        TokenizeMe = new StringTokenizer(Myfile.readLine());
        numTokens = TokenizeMe.countTokens();
         
        while (numTokens != 0)
        {
        	for (int WordsinLine=1;
        			WordsinLine<=NumberofTokens;WordsinLine++);
        	{
        		System.out.print(TokenizeMe.nextToken()+" ");
        	}
         
        		System.out.println();
         
        		numWords += NumberofTokens;
         
        		TokenizeMe = new
        				StringTokenizer(MyFile.readLine());
        }
         
        System.out.println("\nThere are " + NumberofWords + " words in the text");
         
        MyFile.close();
        }
        		}
        		}
        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?

        Code:
          import java.io.*;
        import java.util.*; 
        public class App {
         //Create BufferedReader class instance
         public static InputStreamReader input = new InputStreamReader(System.in);
         public static BufferedReader keyboardInput = new BufferedReader (input);
         public static void main(String[] args) {
          int[] lengths = new int[100]; //Runtime error if the file contains a word of more than 100 characters
          int longestWord = 0;
        		for(int i = 0; i < lengths.length; i++) {
           lengths[i] = 0;
          }
          try {
           System.out.println("Enter the name of the file you wish to read:  ");
           String fileName = keyboardInput.readLine();
           FileReader file = new FileReader(new File(fileName));
           BufferedReader myFile = new BufferedReader(file);
           int numTokens = 0;
           int numWords = 0;
           int numChar= 0;
           String line;
           while ((line = myFile.readLine()) != null ) {
        	String[] words = line.split(" ");
        	numWords = numWords + words.length;
        	for (int i = 0; i < words.length;i++) {
        	 numChar = numChar + words[i].length();
        	 if(words[i].length() > longestWord) {
        	  longestWord = words[i].length();
        	 }
        	 lengths[words[i].length()] = lengths[words[i].length()] + 1;
        	}
           }
           System.out.println("\nThere are " + numWords + " words in the text file");
           System.out.println("\nThere are " + numChar + " characters in the text file");
           System.out.println("\nThere length frequency of the words is as follows:");
           System.out.println("\nLength   :  Frequency ");
           for(int i = 0; i <= longestWord; i++) { //Print frequencies only up to the longest word
        	System.out.println(i + "	 :"+lengths[i]);
           }
           myFile.close();
          }
          catch(IOException iO) {
           iO.printStackTrace();
          }
         }
        }

        Comment

        Working...