2016-12-14 3 views
0

"FileHandler.java"のGETTERを取得して、 "main.java"に値(final_match)を返し、putを出力しようとしています。問題は、正しい値を持つfinal_match私のゲッターの外側「FileHandler.java」は0としてfinal_match出力ということですが、呼び出されたときに/ゲッター値から返さJava SetterとGetter

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

出力と出力はどれくらいですか? – shmosel

+0

ゲッターが更新前に実行されています。スレッドが最初に完了するのを待つ必要があります。 – shmosel

+0

@shmoselもし私が尋ねることができるのであれば、それはどのように行われますか –

答えて

0

あなたfinal_matchは、スレッドが開始するので、出力時の0であると、まだfinal_matchのときの出力値を完了していません。

+0

もし私がこの問題を解決する方法を教えてください。 –

+0

例えば、宣言を変更することができます。ブール値にdone = false;あなたはFileHandlerが既に完了状態で開始していないようにします。 – rob

関連する問題