Word Count plz help

counting number of words in a text file

Page 1 of 1

5 Replies - 2215 Views - Last Post: 23 April 2010 - 04:28 PM Rate Topic: -----

#1 lostgirl21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-February 09

Word Count plz help

Posted 23 April 2010 - 08:26 AM

Hi,
I am having problems counting the number of words in a text file that i created (it has 130) and printing that finding (number) into another text file (output)....Any help is welcomed. I have this, but it doesn't print anything out yet...am stuck.

import java.io.*;

public class  FileIO{

		public static void main(String[] args)throws IOException {
			
		}
			public static int WordCount(String line) throws IOException {
		        FileReader inputStream = null;
		        FileWriter outputStream = null;

		        try {
		            inputStream = new FileReader("G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
		            outputStream = new FileWriter("G:\\Java 1302\\project\\FILEIO\\src\\output.txt");

		            int numWords = 0;
		            int index = 0;
		            boolean prevWhiteSpace = true;
		            while(index < line.length()){
		              char c = line.charAt(index++);
		              boolean currWhiteSpace = Character.isWhitespace(c);
		              if(prevWhiteSpace && !currWhiteSpace){
		                numWords++;
		                
		              }
		              prevWhiteSpace = currWhiteSpace;
		            }
		            return numWords;
		         		           
		        } finally {
		            if (inputStream != null) {
		                inputStream.close();
		            }
		            if (outputStream != null) {
		                outputStream.close();
		            }
		        }
			
		}

	}



Is This A Good Question/Topic? 0
  • +

Replies To: Word Count plz help

#2 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Word Count plz help

Posted 23 April 2010 - 08:36 AM

Hello,

It looks like you are opening the streams and then closing them, but you don't actually do anything with them in between that. Perhaps you should take a look here in order to get a better understanding of how you should use streams.

~Torvos~

Edit: You may want to comment out most of the logic with counting the words at first. Start by just trying to read "Hello World" from one file and writing it to the other.

This post has been edited by xTorvos: 23 April 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 lostgirl21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-February 09

Re: Word Count plz help

Posted 23 April 2010 - 08:56 AM

thanks i will...
Was This Post Helpful? 0
  • +
  • -

#4 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Re: Word Count plz help

Posted 23 April 2010 - 10:33 AM

Do you know that your program never gets inside the code that you have written:

It compiles only
 public static void main(String[] args)throws IOException {
                        
                }


Learn the correct use of braces
Was This Post Helpful? 0
  • +
  • -

#5 lostgirl21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-February 09

Re: Word Count plz help

Posted 23 April 2010 - 04:21 PM

View Postsingularity, on 23 April 2010 - 09:33 AM, said:

Do you know that your program never gets inside the code that you have written:

It compiles only
 public static void main(String[] args)throws IOException {
                        
                }


Learn the correct use of braces

Was This Post Helpful? 0
  • +
  • -

#6 lostgirl21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-February 09

Re: Word Count plz help

Posted 23 April 2010 - 04:28 PM

i have made progress, i have changed it to this but it is counting 136 not 133 as i already know the word count...and how do i count for vowels?
this is my code now

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();
			}
		}

	}

}


This post has been edited by lostgirl21: 23 April 2010 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1