# Array prints line 2x

• 07-13-2013, 08:23 PM
Cinnic
Array prints line 2x
My code works "almost" correctly. It accurately prints the line that appears more than once in its given array. However, I don't know how to stop it from printing the same number more than once. In the text file it reads from, the following numbers appear more than once: 147, 258, & 369 but my code make it display like this:
147
258
369
369
258
147

I would certainly appreciate some help solving this problem. Thanks!

Code:

```    //check enrolled student ID's appearing > 1     System.out.println("These enrolled student's Id # appear more than once:");         for(int i=0; i < numEnrolled; i++)                    //cycle to next enrolled Id #       {         int studentId = enrollStdIds[i];                  //assign current Id # to interger         int counter = 0;                                  //initialize counter             //itterate over attending array             for(int j=0; j < numEnrolled; j++)            //cycle to next enrolled Id #               {                 if (enrollStdIds[j] == studentId)        //increase counter if matching                   {                     counter++;                            //accumulator                   }                 if (counter == 2)                        //execute if counter =2                   {                     System.out.println(studentId);        //display Id #                     counter = 0;                          //reset counter                   }               }         }```
• 07-13-2013, 08:41 PM
Re: Array prints line 2x
so if i have understand correct you read a file with numbers on each line..
and you want a program which will print all the numbers which appears more than once.. so you can create an ArrayList in which you will but all the numbers which appears more than once and befor you search if the next number in next line appear more than once you will first check if this number is already in ArrayList so you will void it ..
• 07-13-2013, 09:04 PM
jim829
Re: Array prints line 2x
You could also sort the array. Then iterate thru the list, checking to see if the next number is the same as the current one, and then skipping all subsequent identical numbers.

Regards,
Jim
• 07-13-2013, 09:07 PM
Toll
Re: Array prints line 2x
Hmm. Why do you reset the counter after printing an ID that appears more than once?
• 07-14-2013, 12:19 AM
Cinnic
Re: Array prints line 2x
I attempted to place the duplicate numbers in their own array and print from that but, stuck again.....

Code:

```//check enrolled student ID's appearing > 1 System.out.println("These enrolled student's Id # appear more than once:");   for(int i=0; i < numEnrolled; i++)                    //cycle to next enrolled Id #   {     int studentId = enrollStdIds[i];                  //assign current Id # to interger     int counter = 0;                                  //initialize counter     int[] twiceIDs;                                  //create array for duplicate ID#           //itterate over enrolled array         for(int j=0; j < numEnrolled; j++)            //cycle to next enrolled Id #           {             if (enrollStdIds[j] == studentId)        //increase counter if matching               {                 counter++;                            //accumulator               }             if (counter == 2 && studentId != 0)      //execute if counter = 2               {                 twiceIDs = new int[numEnrolled];                 Arrays.sort(twiceIDs);                 System.out.println(twiceIDs);                 counter = 0;                          //reset counter               }           }     }```
• 07-14-2013, 03:15 PM
Re: Array prints line 2x
what about trying something like that?

Code:

```import java.util.Scanner; import java.util.ArrayList; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Collections; class DoubleNumbers{         public static void main(String [] args){                 Scanner inputReader = null;                 try {                         inputReader = new Scanner(new FileInputStream("myText.txt"));                 }catch(FileNotFoundException e){                         System.out.println("File Not Found!");                         System.exit(0);                 }                 ArrayList<Integer> numbers = new ArrayList<Integer>();                 while(inputReader.hasNext()){                         String line = inputReader.nextLine();                         numbers.add(Integer.parseInt(line));                 }                 Collections.sort(numbers);                 int x ;                 int y;                 ArrayList<Integer> doubleNumbers = new ArrayList<Integer>();                 for(int i = 0 ; i < numbers.size() -1; i ++){                         x = numbers.get(i);                         y = numbers.get(i+1);                         if(x == y & !doubleNumbers.contains(x)){                                 doubleNumbers.add(x);                         }                 }                 for(int i = 0 ; i< doubleNumbers.size() ; i ++){                         System.out.println(doubleNumbers.get(i));                 }         } }```

i make a file with name myText.txt with this inside :
100
500
900
600
400
100
584
600
665
584
901
584
901
584
901
600
500

and then i make an ArrayList<Integer> which i will put all the lines of the text above
then i sort the ArrayList just to look like this :

100
100
400
500
500
584
584
584
584
600
600
600
665
900
901
901
901

then i have create an ArrayList doubleNumbers in which i will keep all numbers which exist more than once in
the text file..

last i just check if element in index i in numbers ArrayList is the same in index i + 1 and if the element in
index i exist in doubleNumbers ArrayList.. because if already Exist i will no put it in DoubleNumbers ArrayList again..
• 07-14-2013, 03:22 PM
JosAH
Re: Array prints line 2x
Arrays and all sorts of lists are just a clumsy way of thinking about this problem; use a Set instead because a Set only stores each element once.

kind regards,

Jos