aspose file tools*
The moose likes Beginning Java and the fly likes How to compare the new student with ArrayList. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to compare the new student with ArrayList." Watch "How to compare the new student with ArrayList." New topic
Author

How to compare the new student with ArrayList.

Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
Hello there,
I want to know how to compare the new student that user add, with the one in the arrayList.
If the user add the same studentID , with the one in the ArrayList. It will appear Duplicate Students.
But If the studentId is not exist, then it will add the student into the ArrayList.

Below is my code. hope you guys can help me to fix it.
Thank you.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
What is your question or problem?

You say "help me fix it", but you don't tell us what is wrong. Are we supposed to run the code and try to figure out what is wrong? Does it work, but not as well as you want? Does it detect student id but you want it to detect other things? Do you want a version that will operate in Swing? AWT? Urdu?

The forums work best when you ask a detailed question. If you give us a program and only say "help me fix it", you may not get any responses, not even this one.

rc
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
okay ..sorry for that.

when i add the new student that is not exist in ArrayList. it said Duplicate Students.

I want to compare the student that I add with the ArrayList.
If it already exist, it will say Duplicate Students,
If not it will add the student.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Oh. You are comparing a value from the array list with the same value from the array list. Those will always be equal.

In your comparison, perhaps you meant to compare the student id from the array list with the one that had been input?

rc
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
oky thanks.
but if i do it like this. it will loop the new student.
OUTPUT:
Duplicate students
StudentID Name Year
S110 Ralph Derby 1987
S123 June Forster 1974
S134 Susie Karl 1960
S220 Phillip Gregg 1958
S240 Henry Wine 1984
s333 Saya 1234
s333 Saya 1234
s333 Saya 1234
s333 Saya 1234
s333 Saya 1234


Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Your "if - else" is within your loop -- I think what you meant was to loop through all the students, and only IF you do not find a duplicate, THEN enter the new student in. Since the if - else is within, the else is going to be executed as many times as there are students in the loop.

You need to find a way to go through the whole loop and then determine whether a duplicate was found. Then test that to determine whether to put up a message or add the new student.

rc
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
yup exactly.
thanks for that.
i will try to figure this out.
thanks
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

anne annetote wrote:yup exactly.
thanks for that.
i will try to figure this out.
thanks


Why not use a Set instead, and override equals() and hashcode(), so it can take care of duplicates for you?
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
Thanks for the reply.
Set? how to use it.
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

Anne Saizan wrote:Thanks for the reply.
Set? how to use it.


Yes, another type of Collection that doesn't allow duplicates. But also, they're not identified with an index, meaning you can't use get() and some other functions on it.

For your implementation would be to override equals() and hashCode() on your Student class
Could be as follow:


Given that, everytime you try to add an element to the set it will perform a check using the hashCode() then the equals() functions to check whether the two given objects are the same. If a duplicate is found, add will return false.

PD: I just gave you an example code, you can use your own implementation as desired
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Ronald Castillo wrote: . . .
For your implementation would be to override equals() and hashCode() on your Student class
Could be as follow:
. . .
That is unreliable; it will throw an Exception for anything but a Student object. The equals() method must accept any type of object, including null as an argument. And it seems odd that you are only using the ID as a test whether the Students are equal. Here is a recent thread where I wrote about equals methods. We have an FAQ and there are other good references, which you can find from this post.
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

That is unreliable; it will throw an Exception for anything but a Student object. The equals() method must accept any type of object, including null as an argument. And it seems odd that you are only using the ID as a test whether the Students are equal. Here is a recent thread where I wrote about equals methods. We have an FAQ and there are other good references, which you can find from this post.


I know the way I did it wasn't perfect but it was just meant to be a simple example. Regarding the ID, it may be unique, since it's why I only compared the ID. Again, this is only a simple example to see how the Set checks for duplicates.

Thanks for the hints ;)
 
 
subject: How to compare the new student with ArrayList.
 
Similar Threads
Question on ArrayList
Not able to submit Ordered List even using Indexed property
getting java.lang.NoSuchMethodError: main error! thanks =)
new elements in the arraylist
What causes these two errors?