I am working on a homework assignment due next week. We are supposed to have three threads. The first one reads text from a text file, the next one reverses every other word and does not include punctuation in the reversal, and the last thread takes the reversed words from the queues and writes them back to a new text file.
The BlockingQueues can only be of length 2. I have successfully read from the file and reversed the words. However I am having trouble writing to a text file. So far it is only writing the first word. Here is the code in the final output thread's run method. I am not sure how to have the thread write the two words currently in the queue and then stop and wait for the queue to fill up with new reversed words from the other thread
public void run() {
while(true){
try {
FileWriter writer = new FileWriter(file);
try {
writer.write(out.take() + " ");
} catch (InterruptedException ex) {
Logger.getLogger(outputClass.class.getName()).log(Level.SEVERE, null, ex);
}
writer.close();
} catch (IOException ex) {
Logger.getLogger(outputClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}