Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: counting words of a text file

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default counting words of a text file

    Hi,
    I am having problems counting the number of words in a text file that i created (it has 130...input.txt) and printing that finding (number) into another text file (output)....Any help is welcomed. I have this, but it prints individual counts not a whole..and my other question is how would i print vowel/count them 2.?

    iimport java.io.*;
     
    public class FileIO {
    	 public static void main(String[] args) {
     
    		String filename = "G:\\Java 1302\\project\\FILEIO\\src\\input.txt"; // Relative pathname -- place in project directory
    		String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt"; // String to hold the contents eventually
     
    		try {
     
    		FileReader fr = new FileReader(filename);
    		BufferedReader br = new BufferedReader(fr);
    		String lineRead = br.readLine();
    		while(lineRead != null){
    		fileContents = fileContents + "\n" + lineRead;
    		lineRead = br.readLine();
    				}
     
    		br.close();
     
    		} catch (FileNotFoundException e) {
    		e.printStackTrace(); // Display the error
    		System.exit(1); // Stop the program from running
    		} catch (IOException e) {
    		e.printStackTrace();
    		System.exit(1);
    		}
     
    		String[] wordList = fileContents.split("\\s");
    		int size = wordList.length;
     
    		String [] words;
    		int [] counts;
    		words = new String [size];
    		counts = new int [size];
     
     
    		for(int i = 0; i < size; i++){
    		words[i] = wordList[i];
    		for(int j = 0; j < size; j++){
    		if(words[i].equals (wordList[j])){
    		counts[i] = counts[i] +1;
    		}
    		}
    		System.out.println( " " + counts[i]);
    		}
     
    		String outputFilename = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
     
    		try {
    		FileWriter fw = new FileWriter(outputFilename);
    		BufferedWriter bw = new BufferedWriter(fw); // Use bw.write to write to the file
     
    		for(int i = 1; i < size; i++){
    		bw.write(words[i] + ":" + counts[i] + "\n");
    		}
    		bw.close(); // Always remember to close the file
    		} catch (IOException e) {
    		e.printStackTrace();
    		System.exit(1);
    		}
    	 }
     
     
    	 }
    Last edited by maybach230; April 23rd, 2010 at 06:25 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: counting words of a text file

    Change the regex of split to also include new line characters...
    String[] wordList = fileContents.split("[\\s\\n]");

    Or, rather than placing a new line into the string when reading the file, just place a space

    fileContents = fileContents + "\s" + lineRead;

    For optimization's sake, its a bit better practice to use a StringBuilder or StringBuffer rather than 'addition' of Strings.

  3. The Following User Says Thank You to copeg For This Useful Post:

    maybach230 (April 23rd, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: counting words of a text file

    i have this now.it's counting 136 not 133 because i kno the number of words in my file...and how do i count vowels?

    this is what i have

    import java.io.*;
    import java.util.regex.*;
     
    public class FileIO {
    	public static void main(String[] args) throws IOException {
    		countWords();
    	}
     
    	public static void countWords() throws IOException {
    		FileReader inputStream = null;
    		FileWriter outputStream = null;
     
    		String filename = "G:\\Java 1302\\project\\FILEIO\\src\\input.txt"; 
    		String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\input.txt"; 
     
    		try {
     
    			inputStream = new FileReader(
    					"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
    			outputStream = new FileWriter(
    					"G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");
     
    			BufferedReader br = new BufferedReader(inputStream);
    			String lineRead = br.readLine();
     
    			while (lineRead != null) {
    				fileContents = fileContents + "\n" + lineRead;
    				lineRead = br.readLine();
    				// count words and display the count
     
    				String[] wordList = fileContents.split("[\\s\\n]");
    				int size = wordList.length;
    				// display on console word count
    				System.out.println(" the total world count is "
    						+ wordList.length);
    				// print into new file
    				outputStream.write(" the total world count is "
    						+ wordList.length);
     
    			}
    		} finally {
    			if (inputStream != null) {
    				inputStream.close();
    			}
    			if (outputStream != null) {
    				outputStream.close();
    			}
    		}
     
    	}
     
    }
    Last edited by maybach230; April 23rd, 2010 at 10:41 PM. Reason: adding more info

  5. #4
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: counting words of a text file

    Sound good!

  6. #5
    Junior Member
    Join Date
    May 2010
    Location
    India
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: counting words of a text file

    Check where the while loop closing brace is, because in the code posted, it looks to be at incorrect position.
    Only below part should be in while loop:
    fileContents = fileContents + "\n" + lineRead;
    lineRead = br.readLine();
    To investigate further, print all the words in wordList array. It sometimes happens that the last line of file is read twice.
    Also, the fileContents variable is not initialized to blank String (""). It is full file name.
    One more point, why is the input and output file name same?

Similar Threads

  1. outputing to a text file from sound API
    By bondage in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 2nd, 2010, 11:43 AM
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 08:36 AM
  4. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM