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.?
Code :
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);
}
}
}
Re: counting words of a text file
Change the regex of split to also include new line characters...
Code :
String[] wordList = fileContents.split("[\\s\\n]");
Or, rather than placing a new line into the string when reading the file, just place a space
Code :
fileContents = fileContents + "\s" + lineRead;
For optimization's sake, its a bit better practice to use a StringBuilder or StringBuffer rather than 'addition' of Strings.
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
Code :
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();
}
}
}
}
Re: counting words of a text file
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:
Code :
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?