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

	}

what 's it doing so far? are you getting wrong output, is there a problem writing the file, are you getting error messages?

what 's it doing so far? are you getting wrong output, is there a problem writing the file, are you getting error messages?

I have changed it to this now

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

		
	 }

my output is the individual counts of the words and i need the sum, sum i can do....my question is how do i count vowels now?

Have you looked at using RegEx's?

i.e.

import java.util.regex.*;
....
    String regexp = "\\\\W";  
    String[] words = content.split(regexp);
    System.out.println("Total words: " + words.length);
....

Syntax maybe messed-up, but I am not in front of a jdk-installed machine.

HTH's

for the vowels try this:

String regexp = "[aeiou]";

with a pattern match...

HTH's

i have never here of that, but let me read up on it.. thanks

Have you looked at using RegEx's?

i.e.

import java.util.regex.*;
....
    String regexp = "\\\\W";  
    String[] words = content.split(regexp);
    System.out.println("Total words: " + words.length);
....

Syntax maybe messed-up, but I am not in front of a jdk-installed machine.

HTH's

for the vowels try this:

String regexp = "[aeiou]";

with a pattern match...

HTH's

is there a way to do it without regexp?

It is also a good time for you to do learn about performance testing... Compare the performance of the looping vs regex versions of your application. Good Luck!

is there a way to do it without regexp?

I used

System.out.println( " " + words.length);

which is printing out the total word length, the problem is that it is printing it out multiple time, i need just one...how do i fix this

this is my code

String[] wordList = fileContents.split("\\s");
		int size = wordList.length;
		int totalcounts=0;

		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( " " + words.length);
		}

is there a way to do it without regexp?

Tell ya what, maybe the quickest method is to the load the document up in memory and use the java.lang.String split function (check it out here: java.lang.String...

....
String[] strArray;
String strFileData = '';  //Need to look into Load File into String Code
strArray = strFileString.split("\\s+");
System.out.println("Total words: " + strArray.length);
....

Here are some File to String codes: Java String from File

Hope that puts you in the right direction...

I used

System.out.println( " " + words.length);

which is printing out the total word length, the problem is that it is printing it out multiple time, i need just one...how do i fix this

this is my code

String[] wordList = fileContents.split("\\s");
		int size = wordList.length;
		int totalcounts=0;

		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( " " + words.length);
		}

You only need the total count of words correct?
Then why are you using the for loops?
You already have the count once you issue the split and do a length on it.... As long as fileContents has the string of the entire document.

You only need the total count of words correct?
Then why are you using the for loops?
You already have the count once you issue the split and do a length on it.... As long as fileContents has the string of the entire document.

so i just take out this:

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

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

	}

}

and it prints into the other file (output) the wordcount but it's off..the word count is suppose to be 133 not 136.....i thought i got rid of counting the spaces... HElp please...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.