• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to compare the new student with ArrayList.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup exactly.
thanks for that.
i will try to figure this out.
thanks
 
Ranch Hand
Posts: 47
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
Set? how to use it.
 
Ronald Castillo
Ranch Hand
Posts: 47
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 47
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ;)
 
reply
    Bookmark Topic Watch Topic
  • New Topic