14 Replies Latest reply: Nov 8, 2006 9:49 AM by 807599 RSS

    Array Output

    807599
      Ok new problem now. I have to get the duplicates to go into a new array in order to display the duplicates. I can't figure out why it just lists the one duplicate but not the others.(It lists the final duplicate)

      Here is my code:

      import java.util.*;
      import java.io.*;
      import javax.swing.*;

      class algorithmAssignment1{
           public static void main (String[] args)throws IOException{
           
           int mark, counter, arrayAsize, mark2,counter2;
           counter=0;
           arrayAsize=100;
           counter2=0;
           
           int [] A= new int[arrayAsize];
           int [] B= new int[arrayAsize];
           int [] C=new int [arrayAsize];

      String fileName = "arraynumbersa.txt";
      String value,value2;
      BufferedReader input;

      input = new BufferedReader (new FileReader (fileName));

      value = input.readLine ();

      while (value != null){


      mark = Integer.parseInt (value);

      A[counter]=mark;

      counter=counter+1;

                
      value = input.readLine ();

      }

      String fileName2 = "arraynumbersb.txt";
      BufferedReader input2;

      input2 = new BufferedReader (new FileReader (fileName2));

      value2 = input2.readLine ();     
           
           while (value2 != null){


      mark2 = Integer.parseInt (value2);

      B[counter2]=mark2;

      counter2=counter2+1;

      value2 = input2.readLine ();
      }


           bubbleSort(A,A.length);
           bubbleSort(B,B.length);
           
           System.out.println("Here is list A");
           for (int i = 0; i < A.length; i++){
           System.out.println( A[i] );
           }
      System.out.println();
      System.out.println("Here is list B");
      for (int i=0;i<B.length;i++){
           System.out.println(B);
      }

      for (int i=0;i<A.length;i++){
           for (int j=0;j<B.length;j++){
                if (A[i]==B[j]){
                     for (int k=0;k<C.length;k++){
                          C[k]=(B[j]);
                     }
                     System.out.println("Here"+B[j]);
                }
           }
      }
      System.out.println("Here are A and B");
      for (int i=0;i<C.length;i++){
           System.out.println(C[i]);
           }
      }

      static void bubbleSort(int numbers[], int array_size){
           int i, j, temp;
           for (i = (array_size - 1); i >= 0; i--)
           {
           for (j = 1; j <= i; j++)
           {
           if (numbers[j-1] > numbers[j]){
           
           temp = numbers[j-1];
                               numbers[j-1] = numbers[j];
                               numbers[j] = temp;
                          }
                     }
                }
           }
      }
        • 1. Re: Array Output
          807599
          Please use &#91;code]&#91;/code] tags when posting code, it will keep all your &#91;i's from turning into italics
          • 2. Re: Array Output
            807599
            ok i will but that doesn't fix my problem
            • 3. Re: Array Output
              807599
              uh... no obviously, but it might make it possible for someone else to read your code. at the moment your problem is somewhere in a mess of &#91;i]'s that have disappeared

              and when you repost, make sure you copy from the code itself not from the previous post which has all the formatting stripped already
              • 4. Re: Array Output
                807599
                Then please do it! You can edit Your post

                null
                • 5. Re: Array Output
                  807599
                  Ok here is the code
                  import java.util.*;
                  import java.io.*;
                  import javax.swing.*;
                  
                  class algorithmAssignment1{
                       public static void main (String[] args)throws IOException{
                       
                       int mark, counter, arrayAsize, mark2,counter2;
                       counter=0;
                       arrayAsize=100;
                       counter2=0;
                       
                       int [] A= new int[arrayAsize];
                       int [] B= new int[arrayAsize];
                       int [] C=new int [arrayAsize];
                  
                      String fileName = "arraynumbersa.txt";
                      String value,value2;
                      BufferedReader input;
                        
                      input = new BufferedReader (new FileReader (fileName));
                  
                      value = input.readLine ();
                  
                     while (value != null){
                  
                  
                         mark = Integer.parseInt (value);
                  
                         A[counter]=mark;
                  
                         counter=counter+1;
                  
                            
                         value = input.readLine ();
                         
                       }
                       
                      String fileName2 = "arraynumbersb.txt";
                      BufferedReader input2;
                        
                      input2 = new BufferedReader (new FileReader (fileName2));
                  
                      value2 = input2.readLine ();     
                       
                       while (value2 != null){
                  
                  
                         mark2 = Integer.parseInt (value2);
                  
                         B[counter2]=mark2;
                  
                         counter2=counter2+1;
                  
                         value2 = input2.readLine ();
                       }
                  
                  
                       bubbleSort(A,A.length);
                       bubbleSort(B,B.length);
                       
                       System.out.println("Here is list A");
                       for (int i = 0; i < A.length; i++){
                           System.out.println( A[i] );
                           }
                      System.out.println();
                      System.out.println("Here is list B");
                      for (int i=0;i<B.length;i++){
                           System.out.println(B);
                  }

                  for (int i=0;i<A.length;i++){
                       for (int j=0;j<B.length;j++){
                            if (A[i]==B[j]){
                                 for (int k=0;k<C.length;k++){
                                      C[k]=(B[j]);
                                 }
                                 System.out.println("Here"+B[j]);
                            }
                       }
                  }
                  System.out.println("Here are A and B");
                  for (int i=0;i<C.length;i++){
                       System.out.println(C[i]);
                       }
                  }

                  static void bubbleSort(int numbers[], int array_size){
                       int i, j, temp;
                       for (i = (array_size - 1); i >= 0; i--)
                       {
                       for (j = 1; j <= i; j++)
                       {
                       if (numbers[j-1] > numbers[j]){
                       
                       temp = numbers[j-1];
                                           numbers[j-1] = numbers[j];
                                           numbers[j] = temp;
                                      }
                                 }
                            }
                       }
                  }
                  • 6. Re: Array Output
                    807599
                    tell me what this does:
                    for (int k=0;k<C.length;k++){
                        C[k]=(B[j]);
                    }
                    • 7. Re: Array Output
                      807599
                      Here is your problem.
                      for (int k=0;k<C.length;k++){
                                              C[k]=(B[j]);
                                         }
                      Everytime you find a match you do a loop that copies the matched value into C, starting at element 0 everytime. You effectively overwrite the last copied element with the new one. You only need to copy the duplicate element once, into the next available slot. So you need to keep some counter that doesn't get reset to 0 everytime.

                      Ooops. I just did.

                      Message was edited by:
                      flounder
                      • 8. Re: Array Output
                        807599
                        very good.
                        • 9. Re: Array Output
                          807599
                          ok i did that but because i didn't know how big to make the array the last few values are filled up with the last match. One more thing, how do i stop the array from generating the same value in the array. Say there are three 4's in an array, the C array would record all of the 4s, but i only want to to be recorded
                          • 10. Re: Array Output
                            807599
                            Are you allowed to use Lists and Sets instead of arrays? Because they would make your life a lot easier (a and b Lists, c a Set)
                            • 11. Re: Array Output
                              807599
                              how do i use lists and sets?
                              • 12. Re: Array Output
                                807599
                                i dont think it would matter if i used lists and sets as long as i solve the problem
                                • 13. Re: Array Output
                                  807599
                                  how do i use lists and sets?
                                  You read tutorials, API and text books. When you have a specific problem come back and ask. It would too inefficient for us to teach you on a forum.
                                  • 14. Re: Array Output
                                    807599
                                    Alright i put in a hashset but i have no clue how to make it display integers can someone please help?
                                    Here is my code
                                    import java.util.*;
                                    import java.io.*;
                                    import javax.swing.*;
                                    
                                    class algorithmAssignment1{
                                         public static void main (String[] args)throws IOException{
                                         
                                         int mark, counter, arrayAsize, mark2,counter2,counter3;
                                         counter=0;
                                         arrayAsize=100;
                                         counter2=0;
                                         counter3=0;
                                         
                                         int [] A= new int[arrayAsize];
                                         int [] B= new int[arrayAsize];
                                         int [] C=new int [arrayAsize];
                                    
                                        String fileName = "arraynumbersa.txt";
                                        String value,value2;
                                        BufferedReader input;
                                          
                                        input = new BufferedReader (new FileReader (fileName));
                                    
                                        value = input.readLine ();
                                    
                                       while (value != null){
                                    
                                    
                                           mark = Integer.parseInt (value);
                                    
                                           A[counter]=mark;
                                    
                                           counter=counter+1;
                                    
                                              
                                           value = input.readLine ();
                                           
                                         }
                                         
                                        String fileName2 = "arraynumbersb.txt";
                                        BufferedReader input2;
                                          
                                        input2 = new BufferedReader (new FileReader (fileName2));
                                    
                                        value2 = input2.readLine ();     
                                         
                                         while (value2 != null){
                                    
                                    
                                           mark2 = Integer.parseInt (value2);
                                    
                                           B[counter2]=mark2;
                                    
                                           counter2=counter2+1;
                                    
                                           value2 = input2.readLine ();
                                         }
                                    
                                    
                                         bubbleSort(A,A.length);
                                         bubbleSort(B,B.length);
                                         
                                         System.out.println("Here is list A");
                                         for (int i = 0; i < A.length; i++){
                                             System.out.println( A[i] );
                                             }
                                        System.out.println();
                                        System.out.println("Here is list B");
                                        for (int i=0;i<B.length;i++){
                                             System.out.println(B);
                                    }

                                    for (int i=0;i<A.length;i++){
                                         for (int j=0;j<B.length;j++){
                                              if (A[i]==B[j]){
                                                   for (int k=counter3;k<C.length;k++){
                                                        C[k]=(B[j]);
                                                   }
                                                   counter3=counter3+1;
                                              }
                                         }
                                    }

                                    int [] D=new int [arrayAsize];
                                    //this is where I am having problems with the hash
                                    System.out.println("Here are A and B");
                                    HashSet hs= new HashSet();
                                    hs.add(C);
                                    System.out.println(hs);
                                    Vector bob= new Vector();
                                    bob.add(hs);
                                    System.out.println(bob);

                                    for(int i=0;i<bob.length;i++){
                                         System.out.println(bob[i]);
                                    }

                                    }

                                    static void bubbleSort(int numbers[], int array_size){
                                         int i, j, temp;
                                         for (i = (array_size - 1); i >= 0; i--)
                                         {
                                         for (j = 1; j <= i; j++)
                                         {
                                         if (numbers[j-1] > numbers[j]){
                                         
                                         temp = numbers[j-1];
                                                             numbers[j-1] = numbers[j];
                                                             numbers[j] = temp;
                                                        }
                                                   }
                                              }
                                         }
                                    }