0

I created two classes, a driver and a GUI frame class. In the Driver Class is where i put my arrayList of user objects. But when calling a method from the GUI frame class, the value being returned is 0.

I've tried using a for each loop to compare each object implicitly.

This is the method i'm using to compare the object of temporary users to existing users.

public boolean confirm(boolean loggedIn, int tempStudentNo, int tempPin) {

    //Creation of temporary object for comparison with existing one
    DriverMain temp = new DriverMain(tempStudentNo, tempPin);

    if (getCreation().contains(temp)) {
        System.out.println("user pass");
        setLoggedIn(true);
    } 

    else {
        System.out.println("user fail");
    }
    return loggedIn;

} 

I expect the user to pass. But the result is always "user fail". Any help would be appreciated :)

7
  • 1
    Does DriverMain override equals method? Commented Oct 3, 2019 at 6:47
  • Does your DriverMain class, override the equals method? Commented Oct 3, 2019 at 6:47
  • i don't think so, i didn't override any method in this project Commented Oct 3, 2019 at 6:49
  • 1
    @JonathanCarlos contains uses equals(), so you need to implement value equality in your DriverMain. If you put them in a HashSet (and even if not), you need to override hashcode() as well. Commented Oct 3, 2019 at 6:52
  • could you give me an example please, i think i'd understand it better then Commented Oct 3, 2019 at 6:55

1 Answer 1

1

This answer is based on your response to the comment(s) asking: "Does your DriverMain class, override the equals method?"...

In order to use either an (Array)List, HashTable, HashSet or HashMap, etc. you need to override a "couple" of methods in your DriverMain class. For a List, Set, etc.. and some other Collection Types or Map/HashTable to evaluate if your object is a member of its content, you need to provide it with a way to compare the object you are passing to it and the objects it currently holds.

List
The contains(Object o) method uses the Object's equals(Object o) method to compare your Object o to any Object x (a member of the List Collection).

For more information on this please visit the following link(s): https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

Set
The contains(Object o) method uses the Object's hashCode() and equals(Object o) method to compare your Object o to any Object x (a member of the Set Collection).

For more information on this please visit the following link(s): https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html

Map
The containsKey(Object o) method uses the Object's hashCode() and equals(Object o) method to compare your Object o to any Object x (a member of the Map Interface).

For more information on this please visit the following link(s): https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

Important note There are more ways to approach such a situation, all depending on its implementation. For example: TreeSet and TreeMap do not make use of the hashCode() method.

Sign up to request clarification or add additional context in comments.

2 Comments

Not totally correct. For instance TreeSet/Map don't use hashcode at all, they also use compare() instead of equals(). HashSet/Map uses hashcode and equals.
I'll make this more clear, tried to make this clear in my bottom comment. I agree, it all depends on your implementation, but by "default" this is the most common way of approach. It's clear that the user asking this does not have a lot of experience with Java and will not make use of TreeSet/Map in the future; or any other implementation other then ArrayList, HashSet or HashMap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.