2016-10-01 14 views
2

2つのテキストファイルを2つの別々の配列に格納しました。さて、私は両方の配列を比較して重複値を見つけようとしています。論理に問題があり、重複した値が表示される回数を印刷することができません。2つの配列で重複する値を見つける

FILE1は含まれています

1913 2016 1 1913 186 
2016 1711 32843 2016 518 
3 1913 32843 32001 4 
250 5 3500 6 7 
8 27 73 9 10 
1711 73 11 2 1.4 
1.4 12 33.75278 84.38611 1913 
19 1930 20 21 1947 
22 1955 23 1961 23 
1969 27 1995 26 27 
1962 28 29 30 1970 
31 31 

FILE2は含まれています

1913 2016 32843 31 27 1.4 4 7 2 23 

私はfile1の中で重複しているFILE2内の値を見つけようとしています、と何回。

私は次のコードを持っている:

public static void findDuplicates() { 

     // array for first file 
     for (int n = 0; n < nums.size(); n++) { 

      // matches are false by default 
      boolean match = false; 

      int count = 0; 

       String v = nums.get(n); 

      // array for second file 
      for (int k = 0; k < nums1.size(); k++) { 

       String p = nums1.get(k); 

       // second file contains values from first file 
       if (p.contains(v)) { 

        // there is a match 
        match = true; 

        // when there is a match print out matched values and the number of times they appear in second file 
        if (match) { 

        count++; 

         System.out.println(p + " " + "is duped" + " " + count + " " + "times"); 

        } 


       } 

      } 

     } 


    } 

私がコンパイルし、このコードを実行すると、これが出力されます:

31 is duped 1 times 

誰かが私が間違ってここにやって何を知っているようでしたが?ここで

EDIT

は、私のコードの残りの部分である:

public static ArrayList<String> nums; 
public static ArrayList<String> nums1; 

    //Create a main method to start the program. 
    //Add FileNot FoundException in case the file can't be found by computer. 
    public static void main(String[] args) throws FileNotFoundException{ 

     //The while will help us read the content into our computer piece by piece. It will not stop until the end of assignment.csv. 

     while(FILE1.hasNext()){ 

       //Create a String variable - TempString. We use TempString to store each piece temporarily. 

       String TempString = FILE1.next(); 

       String temp1 = TempString.replaceAll("[\\,]", ""); 

       String pattern1 = "[0-9]+\\.{1}[0-9]+"; 

       //Compile the Regular Expression into Pattern and store it in r1 so that the computer can understand the Regular Expression. 
       Pattern r1 = Pattern.compile(pattern1); 


       Matcher m1 = r1.matcher(temp1); 

       String pattern2 = "[0-9]+"; 

       //Compile the Regular Expression into Pattern and store it in r2 so that the computer can understand the Regular Expression. 
       Pattern r2 = Pattern.compile(pattern2); 


       Matcher m2 = r2.matcher(temp1); 

       nums = new ArrayList<String>(); 



       //Recollect, m1 is used to match decimal numbers. 

       if(!(m1.find())){//if a decimal number CAN'T be found 

        //We use while statement instead of if statement here. 
        //If there is only one piece per line, we can use either while statement or if statement. 
        //However, we have to use while statement if there is more than one piece per line. 
        while(m2.find()) {//if an integer number CAN be found 
         //If an Integer is found, we add 1 to Variable count. 

         count++; 
         //Even though the number (i.e., m2.group(0)) is an Integer, its data type is String. So we store it to a String variable - number. 

         String number = m2.group(0); 


         nums.add(number); 

         //If the remainder of count by 5 is zero, we display the number and advance to a new line. 
         if (count % 5 == 0){ 

          System.out.println(number); 

         } 
         //Otherwise, we just display the number on the same line and divide numbers by a space. 
         else 
          System.out.print(number + " "); 

        } 
       } 

       //If we find a decimal number 
       else{ 
         //We add 1 to Variable count. 



         count++; 

         //Even though the number (i.e., m1.group(0)) is a decimal number, its data type is String. So we store it to a String variable - number. 

         String number = m1.group(0); 

         nums.add(number); 

         //If the remainder of count by 5 is zero, we display the number and advance to a new line. 
         if (count % 5 == 0) { 

          System.out.println(number); 

         } 

         //Otherwise, we just display the number on the same line and divide numbers by a space. 
         else 
          System.out.print(number + " "); 

       } 


     } 



    FILE1.close();//Once we finish the task, we close the file. 

     while(FILE2.hasNext()){ 

      //Create a String variable - TempString. We use TempString to store each piece temporarily. 
      String TempString = FILE2.next(); 


      //So I use replaceAll function to eliminate comma (,) and store the new string in temp1. 
      String temp1 = TempString.replaceAll("[\\,]", ""); 



      String pattern1 = "[0-9]+\\.{1}[0-9]+"; 

      //Compile the Regular Expression into Pattern and store it in r1 so that the computer can understand the Regular Expression. 
      Pattern r1 = Pattern.compile(pattern1); 

      //Match the Regular Expression with the piece (temp1) we read from assignment.csv. 
      Matcher m1 = r1.matcher(temp1); 

      String pattern2 = "[0-9]+"; 

      //Compile the Regular Expression into Pattern and store it in r2 so that the computer can understand the Regular Expression. 
      Pattern r2 = Pattern.compile(pattern2); 

      //Match the Regular Expression with the piece (temp1) we read from assignment.csv. 
      Matcher m2 = r2.matcher(temp1); 

      nums1 = new ArrayList<String>(); 


      //We have two types of numbers - Integer and Decimal 
      //Let's start us Integer. 
      //Recollect, m1 is used to match decimal numbers. 
      if(!(m1.find())){//if a decimal number CAN'T be found 

       //We use while statement instead of if statement here. 
       //If there is only one piece per line, we can use either while statement or if statement. 
       //However, we have to use while statement if there is more than one piece per line. 
       while(m2.find()) {//if an integer number CAN be found 
        //If an Integer is found, we add 1 to Variable count. 


        count++; 
        //Even though the number (i.e., m2.group(0)) is an Integer, its data type is String. So we store it to a String variable - number. 

        String number = m2.group(0); 

        nums1.add(number); 

        //If the remainder of count by 5 is zero, we display the number and advance to a new line. 
        if (count % 5 == 0){ 

         //System.out.println(number); 

        } 
        //Otherwise, we just display the number on the same line and divide numbers by a space. 
        else 
         System.out.println(/*number + " "*/); 

         } 
      } 

      //If we find a decimal number 
      else{ 
       //We add 1 to Variable count. 


       count++; 

       //Even though the number (i.e., m1.group(0)) is a decimal number, its data type is String. So we store it to a String variable - number. 

       String number = m1.group(0); 

       nums1.add(number); 

       //If the remainder of count by 5 is zero, we display the number and advance to a new line. 
       if (count % 5 == 0){ 

        //System.out.println(number); 
       } 
       //Otherwise, we just display the number on the same line and divide numbers by a space. 
       else 
        System.out.println(/*number + " "*/); 

      } 


      findDuplicates(); 


     } 


     FILE2.close();//Once we finish the task, we close the file. 

    } 

私は私ができる限り多くの不要なコードを削除しようとしました。

EDITは

の予想される出力は次のようになります。

1913 is duplicated 3 times. 
2016 is duplicated 2 times. 
32843 is duplicated 1 times. 
31 is duplicated 2 times..... 

EDIT

だから私は、私は問題を発見したと信じています。何らかの理由で、私のfindDuplicates()方法で

String p = nums.get(k) 

は値31、およびない他の値を返しています。私は問題を解決するために取り組んでいます。

+0

numとnum1の配列を実際に作成する場所にコードを追加することもできます。 –

+0

また、期待される出力は何ですか? –

+0

@vatsalmevada – codeREXO

答えて

1

私は最大の問題は、printlineが2番目のforループの内側にあることだと思います。
さらに、私はブール値を削除し、2つの文字列を比較します(p==v)

ので、コードはより次のようになります。意図したとおり、私は、コードの実行を行った変更して

public static void main(String[] args) { 
    // array for second file 
    for (int n = 0; n < nums1.size(); n++) { 

     // matches are false by default 

     int count = 0; 

      String v = nums1.get(n); 

     // array for first file 
     for (int k = 0; k < nums.size(); k++) { 

      String p = nums.get(k); 

      // second file contains values from first file 
      if (p==v) { 

       count++; 

       } 


      } 
     System.out.println(v + " " + "is duped" + " " + count + " " + "times"); 

     } 

    } 

} 


ライブデモhereをチェックアウトすることができます。


出力:番号が重複する回数がprintledされる前に、第2のArrayListの最初の全体が繰り返されますように

1913 is duped 4 times 
2016 is duped 3 times 
32843 is duped 2 times 
31 is duped 2 times 
27 is duped 3 times 
1.4 is duped 2 times 
4 is duped 1 times 
7 is duped 1 times 
2 is duped 1 times 
23 is duped 2 times 
+0

@codeREXOあなたの質問は重複を数える関数についてです。これはここで正しく答えられており、あなたの質問に答えていることを意味しています。あなたの配列の作成が間違っている場合、これはまったく異なる質問なので、そのような質問をする必要があります。 – deW1

0

あなたは内側のループの外System.out.printlnステートメントを使用する必要があります。

ます。また、正しく

プログラムを実行するために、他のいくつかの変更を加える必要がある(int型のn = 0; nは< nums.size(); N ++)、その後{

 // matches are false by default 
     boolean match = false; 

     int count = 0; 

      String v = nums.get(n); 

     // array for second file 
     for (int k = 0; k < nums1.size(); k++) { 

      String p = nums1.get(k); 

      // second file contains values from first file 
      if (p.contains(v)) { 

       // there is a match 
       match = true; 

       // when there is a match print out matched values and the number of times they appear in second file 
       if (match) { 

       count++; 

       match = false; 
       } 
      } 
      System.out.println(p + " " + "is duped" + " " + count + " " + "times"); 
      count = 0;    

     } 

    } 

しかし、まだ最初のファイルで何回繰り返したのかを比較していないため、ロジックはすべての場合に機能しません。 2番目のファイル番号と1番目のファイル番号を比較するだけです。私が言及したようにコードを修正した後に2つのファイルを入れ替えて質問した場合、それはうまくいくでしょう。

0

試してみてください。

package stackoverflow.test; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class Test { 

    public List<Integer> list = new ArrayList<Integer>(); 

    public List<Integer> dup = new ArrayList<Integer>(); 
    public Map<Integer, Integer> hashDup = new HashMap<Integer, Integer>(); 

    public void fileReader() { 

     File file = new File("/home/john/Documents/file1.txt"); 
     List<Integer> list1 = this.output(file); 
     File file2 = new File("/home/john/Documents/file2.txt"); 
     List<Integer> list2 = this.output(file2); 
     for (int i = 0; i < list1.size(); i++) { 
      int counter = 0; 
      for (int j = 0; j < list2.size(); j++) { 
       if (list1.get(i) == list2.get(j)) { 
        counter++; 
       } 
      } 
      if (!hashDup.containsKey(list1.get(i))) { 
       hashDup.put(list1.get(i), counter); 
       System.out.println(" dup " + list1.get(i) + " :" + counter); 
      } 

     } 
    } 

    public List<Integer> output(File file) { 
     BufferedReader reader = null; 

     try { 
      reader = new BufferedReader(new FileReader(file)); 
      String text = null; 

      while ((text = reader.readLine()) != null) { 
       // System.out.println(text); 
       String[] str = text.split(" "); 
       for (String string : str) { 
        list.add(Integer.parseInt(string)); 
       } 

      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
      } 
     } 

     // print out the list 
     // System.out.println(list.toString()); 

     return list; 
    } 

    public static void main(String args[]) { 

     Test t = new Test(); 
     t.fileReader(); 
    } 
} 
関連する問題