1

I'm a beginner in Java and I have a problem with printing duplicate rows. I have this code:

while (rs.next()) {

    String curr_crn = rs.getString(2);

    while(rs.next()) {
        if(curr_crn != rs.getString(2)) {
            String instructor = getInstructor(connection, 
                rs.getString(1),rs.getString(2));
            String student = getStudent(connection,rs.getString(2));

            detailLines.add(Library.rPad(rs.getString(1), COL1, ' ')
                + Library.rPad(rs.getString(2), COL2, ' ')
                + Library.rPad(rs.getString(3) + " " + rs.getString(4) + 
                    " " +rs.getString(5), COL3, ' ')
                + Library.rPad(instructor, COL4, ' ')
                + newline
                + Library.rPad(student, COL1 ,' '));
        }
    }
}

How do I compare current crn to the other crn so that I won't print duplicates?

thanks!

Adil B
  • 14,635
  • 11
  • 60
  • 78
  • 6
    Do `SELECT DISTINCT` to avoid duplicate rows. – jarlh Oct 16 '18 at 14:24
  • 1
    for this assignment, we're not allowed to change the sql query, so I have to do in the java program –  Oct 16 '18 at 14:27
  • Add the results to a set, for example a `HashSet`. It doesn’t accept duplicates, so duplicates added to it will be ignored. – Ole V.V. Oct 16 '18 at 14:29

5 Answers5

1

Inner while loop can be removed as it makes the code complex. You can create a HashSet object. Then you can start while loop and store the current_crn in the set in the last line in each iteration after details are printed. Code should be similar to the below:

HashSet hs = new HashSet();
while (rs.next()) {
        String curr_crn = rs.getString(2);
            if( ! hs.contains(curr_crn) ) {
                String instructor = getInstructor(connection, 
                                                rs.getString(1),rs.getString(2));
                String student = getStudent( connection, 
                                                  rs.getString(2));

                detailLines.add(Library.rPad(rs.getString(1), 
                                         COL1, ' ')
                     + Library.rPad(rs.getString(2), COL2, ' ')
                     + Library.rPad(rs.getString(3) + " " + 
                          rs.getString(4) + " " +
                          rs.getString(5), COL3, ' ')
                     + Library.rPad(instructor, COL4, ' ')
                       + newline
                        + Library.rPad(student, COL1 ,' '));
                   hs.add(curr_crn);
          }
       }
Mohan
  • 334
  • 2
  • 12
0

Select Distinct then you will not recieved duplicate elements

GauravRai1512
  • 834
  • 6
  • 14
0

before picking up the values, in your query, you can use DISTINCT

SELECT DISTINCT column1, column2, ... FROM table_name;

SELECT DISTINCT returns only distinct (different) values. SELECT DISTINCT eliminates duplicate records from the results. DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.

You can try hash too, like that: How does HashSet not allow duplicates?

FoXgF
  • 1
  • 2
0

My first idea would be to review the SQL query and analyze why there are duplicates. Since you say that query is untouchable for you, we will assume it just returns what it must return.

In this case, I would recommend you to separate your data access from the logic. The data are the data, and we assume they are ok: just return them as a List of beans. This is our data layer (some people call it the model).

Now our objective is to present the data without duplicates. Another class, which constitutes our presentation layer (some people call it the view) can do this filter. For example, here you have an explanation of how to filter duplicates in a List with the streams added to Java 8: https://www.baeldung.com/java-remove-duplicates-from-list

I hope you find this useful.

Jorge_B
  • 9,712
  • 2
  • 17
  • 22
0

Use a HashSet to store the unique crns and process the row only if the current crn does not exist in the HashSet

    Set<String> uniqueCrns = new HashSet<>();
    while (rs.next()) {

        String curr_crn = rs.getString(2);

        // check for duplicates
        if (uniqueCrns.contains(curr_crn)) {
            continue;
        }

        // the curr_crn is unique add it to the unique set and process it
        uniqueCrns.add(curr_crn);

        String instructor = getInstructor(connection,
                rs.getString(1),rs.getString(2));
        String student = getStudent(connection,rs.getString(2));

        detailLines.add(Library.rPad(rs.getString(1), COL1, ' ')
                + Library.rPad(rs.getString(2), COL2, ' ')
                + Library.rPad(rs.getString(3) + " " + rs.getString(4) +
                " " +rs.getString(5), COL3, ' ')
                + Library.rPad(instructor, COL4, ' ')
                + newline
                + Library.rPad(student, COL1 ,' '));

    }