1

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 :)

Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27

1 Answers1

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.

  • 1
    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. – Kayaman Oct 03 '19 at 07:11
  • 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` – Remco Buddelmeijer Oct 03 '19 at 07:13