2016-12-14 33 views
0

我想获取“FileHandler.java”的GETTER将值(final_match)返回到“main.java”并输出。问题是,final_match输出作为0。在“FileHandler.java”我的吸气剂final_match具有正确的值之外,但调用时/从吸气返回值是纯0Java安装程序和Getter

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) 
+1

您的输出和预期输出是什么? – shmosel

+0

更新前您的getter正在运行。您需要等待线程先完成。 – shmosel

+0

@shmosel如果我可以问,那是怎么做的 –

回答

0

你final_match是0在输出的时候,因为线程启动,还没有完成,当你输出final_match的价值。

+0

如果我可能会问,我该如何解决这个问题 –

+0

你可以在例子中改变声明布尔值done = true;布尔值done = false;这样你的FileHandler就不会以完成状态启动了。 – rob