# Thread: Array prints line 2x

1. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## 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!

Java 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
}
}
}```

2. Member
Join Date
Mar 2013
Location
Greece
Posts
94
Rep Power
0

## 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 ..

3. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,729
Rep Power
4

## 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

4. ## Re: Array prints line 2x

Hmm. Why do you reset the counter after printing an ID that appears more than once?

5. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Re: Array prints line 2x

I attempted to place the duplicate numbers in their own array and print from that but, stuck again.....

Java 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
}
}
}```

6. Member
Join Date
Mar 2013
Location
Greece
Posts
94
Rep Power
0

## Re: Array prints line 2x

what about trying something like that?

Java 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){
try {
}catch(FileNotFoundException e){
System.exit(0);
}
ArrayList<Integer> numbers = new ArrayList<Integer>();
}
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)){
}
}
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..
Last edited by ShadowWalker; 07-14-2013 at 04:24 PM.

7. ## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•