I am trying to get the GETTER of "FileHandler.java" to return the value(final_match) to "main.java" and out put. Problem is that final_match outputs as 0. In "FileHandler.java" Outside of my getter final_match has the correct value, but when called/returned from the GETTER value is plain 0
main.java
package textreader;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class main {
public String data = "";
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
final File directory = new File("E:\\test\\filename.txt");
// final File directory2 = new File("E:\\test\\filename2.txt");
ArrayList<File> f = new ArrayList<>();
f.add(directory);
// f.add(directory2);
final String words;
System.out.println("The word you would like to search for. ");
System.out.println("----------------------------------------");
words = user_input.next();
main aMain = new main(f, words);
}
main(ArrayList<File> f, String words) { //constructor
ArrayList<FileHandler> threadArray = new ArrayList<>();//arraylist of type filehandler
for (File file : f) {
FileHandler fh = new FileHandler(this, words, file);// instance of filehandler = fh
threadArray.add(fh);
fh.start();
for (FileHandler x : threadArray) {
if (x.isFinished()) {
x.setFinished(true);
synchronized (x) {
x.notify();// notify next thread to continue
}
}
int answer = x.getfinal_match(); // CALLING THE GETTER FOR VALUE
System.out.println("----------------------------------------");
System.out.println("this word has appeared: " + answer + " times.");
System.out.println("----------------------------------------");
}
}
}
}
FileHander.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileHandler extends Thread {
BufferedReader br = null;
FileReader fr = null;
String words;
File file;
int counter;
int match;
int final_match;
boolean done = true;
boolean finished;
private final main main;
public FileHandler(main instance, String words, File file) { //constructor
this.words = words;
this.file = file;
this.main = instance;
}
@Override
public synchronized void run() {
try {
fr = new FileReader(this.file);//file path
br = new BufferedReader(fr);// reads the words in the file
String theLine;
while ((theLine = br.readLine()) != null) {
String List[] = theLine.split(" ");
for (String List1 : List) {
if (List1.equals(List[counter])) {// of words are the same as "word" increment match
match++; //Amount of occurrences
}
counter++;//loop through each word
}
synchronized (this) {
//System.out.println("test2 " + match);
this.finished = true;
}
}
} catch (IOException e) {
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException ex) {
}
}
final_match = match;
System.out.println("testing " + final_match); // THIS TEST WORKS AS VALUE WAS OUTPUTTED AS 5
}
public void setfinal_match(int test) {
final_match = test;
}
public Integer getfinal_match() {
System.out.println("testing 123456 " + final_match); // THIS VALUE DOES NOT WORK AS IT OUTPUTS A BIG FAT 0
return final_match;
}
public boolean isFinished() {
return this.finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
}
//OUTPUT
//run:
//the word you would like to search for.
//----------------------------------------
//dog
//testing GETTER 123456 0
//----------------------------------------
//this word has appeared: 0 times.
//----------------------------------------
//testing 5
//BUILD SUCCESSFUL (total time: 2 seconds)
//EXPECTED OUTPUT
//run:
//The word you would like to search for.
//----------------------------------------
//dog
//testing GETTER 123456 5
//----------------------------------------
//this word has appeared: 5 times. (THIS IS THE MAIN VALUE THAT HAS TO CHANGE)
//----------------------------------------
//testing 5
//BUILD SUCCESSFUL (total time: 2 seconds)
Your final_match is 0 at the time of the output, since the thread starts and is not finished yet, when you output the value of final_match.