2016-12-29 4 views
-1

したがって、以下のコードは、単語inputによって特定の文書内の単語を検索します。それぞれの文章で単語が何回出現したかをカウントし、その数をarraylistsの下のラベルaにコーンとbをctwoに格納します。別のクラスでArraylistを使用する

私は別のクラスのarraylistsを使いたいが、それを行う方法を見つけることができないようだ。

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class exc { 

    public exc() { 
     } 
public static void main(String[] args) throws Exception  { 
cone aa = new cone(); 
ctwo bb = new ctwo(); 
// after this I'm stuck 

} 
} 
    import java.io.BufferedReader; 
     import java.io.FileReader; 
     import java.util.Arrays; 
     import java.util.List; 

public class cone { 

public void cone() throws Exception  { 

        BufferedReader e = new BufferedReader(new FileReader("words to be read.txt")); 
         String o; 
         while((o = e.readLine()) != null){ 

          String[] sentences = o.split("\\b[.!?]\\s+"); 

         //System.out.println(o); 
          String [] h = sentences; 

         { 

          BufferedReader t = new BufferedReader(new FileReader("Text to be scan.txt")); 
          String g; 
          while((g = t.readLine()) != null){ 

           String[] set=g.split(" "); 

           List<String> list = Arrays.asList(set); 

          // System.out.println(Arrays.toString(set)); 
           //System.out.println(o); 

          int sentenceNumb=1; 
          for (String sentence: h) { 
           int counter=0; 
           String[] words = sentence.replace(".", "").split(" "); 
           for(String word: words) { 
            if (list.contains(word)) { 
             counter++; 
            } 
           } 


           List<Integer> A = Arrays.asList(counter++); 

          } 


          } 



         } 
         } 

} 
} 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.Arrays; 
import java.util.List; 


public class ctwo { 


public void ctwo() throws Exception  { 

BufferedReader e = new BufferedReader(new FileReader("words to be read.txt")); 
    String o; 
    while((o = e.readLine()) != null){ 

     String[] sentences = o.split("\\b[.!?]\\s+"); 

    //System.out.println(o); 
     String [] h = sentences; 

    { 

     BufferedReader t = new BufferedReader(new FileReader("Text to be scan.txt")); 
     String g; 
     while((g = t.readLine()) != null){ 

      String[] set=g.split(" "); 

      List<String> list = Arrays.asList(set); 

     // System.out.println(Arrays.toString(set)); 
      //System.out.println(o); 

     int sentenceNumb=1; 
     for (String sentence: h) { 
      int counter=0; 
      String[] words = sentence.replace(".", "").split(" "); 
      for(String word: words) { 
       if (list.contains(word)) { 
        counter++; 
       } 
      } 


      List<Integer> B= Arrays.asList(counter++); 

     } 


     } 



    } 
    } 
} 
} 
+0

両方のメソッドが 'ArrayList'sを返すようにしますか? – Jyr

答えて

1
  1. ベストアプローチ:あなたは(メインでのArrayListの両方を持っている)、それらを必要とする(任意のクラスから)関数への関数のパラメータとして渡します。

  2. あまり良いことではありません:ArrayListsをパッケージ保護された静的クラス変数としてconeクラスとctwoクラスに格納します。 cone.Aとctwo.Bとしてアクセスできます。

0

プログラムが奇妙なようです。 私は単語を読んで、重要な単語や値をハッシュマップに追加することをお勧めします。

+0

どうすればいいですか? – DMW18

1

両方のクラスのコンストラクタで同じ配列リストを渡します。

+0

どのように?私はあなたがこれをどうやってjavaに新しいですか? – DMW18

関連する問題