Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Alright, taking a file called test.txt and reading it in to count the words and spit back out into a differt txt file called counts that gives the single words used and how many times they are used each here si my code i have all the counting and word grabbing right i believe just i cant think of a way to leave out the repeated words in the file...
package edu.uwec.cs.cs145.lab4;import java.io.*;
public class WordCounter {
public static void main(String[] args) {
// The following code reads in the contents from a file
// then counts all the words in the file taking into
// account duplicates.String filename = "sample.txt"; // Relative pathname -- place in project directory
String fileContents = ""; // String to hold the contents eventually// "Try" to open up the file with the given filename.
// If an error occurs (like the file isn't found) then it
// "throws an exception" and jumps to the catch section below.
// Otherwise, if no errors occur, it completes all the code in the
// try section and skips over the catch sections.
try {
// Open up the file
// A FileReader can only read one char at a time
FileReader fr = new FileReader(filename);
// Wrap the FileReader in a BufferedReader
// This allows us to read entire lines at a time
// using br.readLine(), which returns the line as a String
BufferedReader br = new BufferedReader(fr);// Read in the entire contents of the file and place them into
// the single string, fileContents.
// Note that if there are no more lines in the file, then the
// String returned by the readLine is == null
// --------YOUR CODE HERE----------
String lineRead = br.readLine();
while(lineRead != null){
System.out.println(lineRead);
fileContents = fileContents + "\n" + lineRead;
lineRead = br.readLine();}
// Close the file after reading
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);
}// Display the contents of the file string
System.out.println(fileContents);// Count the distinct words in the fileContents String
// First dump all the words into an array of strings
// Note that the \\s will split on whitespace (space, tab, newline)
String[] wordList = fileContents.split("\\s");// The words array has been created for us by the split
// and we need to know how many slots have been made in it
int size = wordList.length;// Create two (parallel) arrays for counting the distinct words
// Call these arrays words and counts.// --------YOUR CODE HERE----------
String [] words;
int [] counts;
words = new String [size];
counts = new int [size];
// Go through each word in the word list and count it
//
// First see if the word is on the list of words you have already seen.
// Note that when comparing Strings, you should not use (str1 == str2)
// Instead you should use (str1.equals(str2))
// We will talk more about why in class soon.
//
// If it is not on the list then add it to the end and put in a count of 1
// on the counts list at the end as well.
// Otherwise, increase the word's associated count by 1// --------YOUR CODE HERE----------
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[i] + " " + counts[i]);
}
// Write each distinct word and its associate count to a file
// in the form
// word1: count1
// word2: count2
// ...String outputFilename = "counts.txt";
try {
FileWriter fw = new FileWriter(outputFilename);
BufferedWriter bw = new BufferedWriter(fw); // Use bw.write to write to the file// --------YOUR CODE HERE----------
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);
}}
}
the counts.txt file reads like this
this:3
is:3
a:2
test:3
this:3
is:3
also:1
a:2
test:3
this:3
is:3
the:1
last:1
test:3i just want the single words that are repeated not everyword telling me how many times it was repeated

![]() |
Pipe Find resultes to Var...
|
DOS 'For' Command
|
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |