0

Basically I want it to stop and print a message if they've already added that person to the ArrayList, but it won't do that.

Here's the method:

@Override
    public boolean equals(Object o) {
        if (this.name == ((Student)o).getName() && this.ID == ((Student)o).getID()) {
            return true;
        }
        else {
            return false;
        }
    }

And the code section it's used in:

public void addStudents() {
        Scanner keyboard = new Scanner(System.in);
        String name = "", ID = "";

        System.out.println("Welcome! Please type exit at any point to stop entering students and for the lottery to commence.\n");

        System.out.print("Student name: ");
        name = keyboard.nextLine();

        if (!name.equals("exit")) {
            System.out.print("Student ID: ");
            ID = keyboard.nextLine();
        }

        while (!name.equals("exit") && !ID.equals("exit")) {
            System.out.print("\nStudent name: ");
            name = keyboard.nextLine();

            if (!name.equals("exit")) {
                System.out.print("Student ID: ");
                ID = keyboard.nextLine();

                if (!ID.equals("exit")) {
                    boolean contains = false;

                    for (int i = 0; i < students.size(); i++) {
                        if (students.get(i).equals((new Student(name, ID)))) {
                            contains = true;
                        }
                    }

                    if (!contains) {
                        students.add(new Student(name, ID));
                    }
                    else {
                        System.out.println("You can only enter once.");
                    }
                }
            }
        }
    }

I've toiled over this for quite awhile, but can't put my finger on why it won't work.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • See also [this post](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for a detailed explanation of how to compare strings in Java. – assylias Oct 06 '12 at 20:01

4 Answers4

1

Use the equals() method to compare strings, not ==

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
1

You should also be using String.equals in your Student.equals method:

if (this.getName().equals(((Student) o).getName()) && 
    this.getID().equals(((Student)o).getID())) 

Your Student.equals uses == for String comparison, comparing object references, which will fail if the Strings are lexicographically equal, but not the same String object.

pb2q
  • 58,613
  • 19
  • 146
  • 147
0

There is problem in your equals method..

public boolean equals(Object o) {
    if (this.name == ((Student)o).getName() && this.ID == ((Student)o).getID()) {
            return true;
    }
    else {
           return false;
    }
}

You should compare name using equals() method in this also..

this.name.equals(((Student)o).getName())

And if your ID is also a String, do this for it also..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

Use equals to compare names and ID's. In this case, == comparation boils down to an object comaration that will result in false since both names (each one a different String) are different objects. It will not make a literal comparation.

Just in case, since it's a name comparation you might want to use equalsIgnoreCase because, for example, John Doe is in essence the same name than john doe. Same goes for IDs if they're alphanumeric.

Fritz
  • 9,987
  • 4
  • 30
  • 49