-5

I encountered a problem using equals, let me first add the code here :

Operateur o = new Operateur(nom, age, sexe, grade, role);
for(Operateur op : Membre.getOperateurs()){
  if(o.equals((Object)op)){
    already_exists = true;
    break;
  }
}

if(already_exists){
  ret = ">Can't create it : already exists";
  Membre.removeLast();
  o.finalize();
}else{
  ret = ">Created it";
}

Here I check if a new Operateur isn't just a copy of an old one (Membre.getOperateurs gets me the list of all instances of Membre that are actual Operateur).

My problem is that when I run this bit of code, if my Operateur is brand new it appears as already existing (which he should not, since he's brand new).

For instance, when I create the first Operateur ever it evaluates it as a duplicate of an already existing one.

As :

import cmd.*;
import matrice.*;
import persona.*;
import non_grata.non.*;
import non_grata.grata.*;
//all above is importing packages to use all classes required


public class Debug{
  public static void out(String s){
    System.out.println(s);
  }//handy

  public static void main(String[] args){
    Membre.init();//Membre.membre = new ArrayList<Membre>();
    Personnel.init();//Personnel.membre = new ArrayList<Membre>();
    Vaisseau.init();//Vaisseau.vaisseaux = new ArrayList<Vaisseau>();
    MatrixCmd cmd = new MatrixCmd();//Create the command parser

    String parser_input = "afficherMembres";//get some input
    String parser_output = cmd.execCommand(parser_input);//process it
    out(parser_output);//use it
    //empty list, correct since we only initialized it

    parser_input = "newOPsion mikebike 10 o string string";
    parser_output = cmd.execCommand(parser_input);
    out(parser_output);
    //outputs ">Impossible de créer cet Opérateur : Il existe déjà" which it shouldn't
    //since there are no other Operateur and this is only
    //given as output when it founds a similar Operateur

    parser_input = "afficherMembres";
    parser_output = cmd.execCommand(parser_input);
    out(parser_output);
    //outputs an empty list, meaning that there are no Membre created
    //and therefore no Operateur

    parser_input = "newOPsion mikebke 8 f tring sring";//command that will trigger the bit of code above
    parser_output = cmd.execCommand(parser_input);
    out(parser_output);
    //outputs">Impossible de créer cet Opérateur : Il existe déjà" which it shouldn't
  }
}
Vivick
  • 3,434
  • 2
  • 12
  • 25
  • Oh surpise! When someone yells "possible duplicate" question is marked as duplicate automatically whether it is true or not. I know that because, oh surprise, changing every `==` for `.equals(` in string comparison didn't change a thing \o/. GJ @OliverCharlesworth – Vivick Apr 17 '17 at 23:09
  • @MadProgrammer : It is NOT a duplicate (see above) – Vivick Apr 17 '17 at 23:21
  • 2
    I'm sorry Vivick but even if it's not a duplicate, the question quality is a bit low. Firstly the title is terrible - you've blamed Java for your problems rather than an incomplete understanding. Secondly, you've given quite a big code dump rather than creating an [MCVE]. Thirdly you haven't provided evidence of understanding the difference between the `==` `equals`. If you're asking for free help, please spend some time editing your question with the minimum code for what you are trying to achieve, your research so far, and what happens when you debug it. – David Rawson Apr 17 '17 at 23:28
  • 2
    @Vivick "It is NOT a duplicate" not necessarily. Current duplicate simply solves one of your problems which we could see based on what you showed us. If it doesn't solve all of problems, it is possible they exist in code which you are not showing us. Like David suggested please take your time and try creating [mcve]. – Pshemo Apr 17 '17 at 23:32
  • 2
    @Vivick I'm sorry you feel this way, but based on the initial pass over the out-of-context code, that is an issue you need to correct first, correcting this allows you to solve at least one, albeit, obvious issue which may lead you to discovering the real underlying issue. Based on this correction, feel free to ask a new question with the updated code, providing a runnable example which demonstrates the issue you're trying to solve, otherwise, it becomes nothing more than guess game – MadProgrammer Apr 17 '17 at 23:35
  • 1
    BTW existence of overloaded `equals(YourClass)` method doesn't look right. It forces you to use `if(o.equals((Object)op))` instead of simply `if(o.equals(op))`. If you really want to have helper method maybe use something like `private boolean equalsHelper(YourClass o){...}`. – Pshemo Apr 17 '17 at 23:37
  • Helper didn't help either. The problem resides on the fact that `equals` seems to only evaluate to true – Vivick Apr 18 '17 at 00:01
  • 2
    Purpose of helper method wasn't to solve *current* problem since honestly it is not clear (at least for me). Its purpose was to limit potential *future* problems when you can simply forget to add casting and end up with problem which you will search potentially for hours. Generally casting is considered as symptom of design problem. Anyway in your edit you reduced your code to only some testing method, but we can't really use it (copy-paste and run on our machines) which prevents us from debugging it, so it is still not proper [MCVE] (a.k.a. [SSCCE](http://sscce.org)) – Pshemo Apr 18 '17 at 00:10
  • I can't give you 600 lines of code and tell you "it's minimal, complete and verifiable" – Vivick Apr 18 '17 at 00:23
  • However, there is a part where I say something like (as a comment about `for(Operateur c : Membre.getOperateurs())`) `browse through every created instance of Operateur` which is the alarm signal. You guys couldn't see, and me neither, that `all` means "CURRENT INCLUDED". Therefore, excluding current solved the problem. – Vivick Apr 18 '17 at 00:24
  • How about we all get some glasses – Vivick Apr 18 '17 at 00:33

1 Answers1

1

Change return super.equals((Object)o) && this.role==o.role;, Object.equals tests reference identity (and you know the Objects are unique instances). Also, don't use == for comparing reference types (like String). I think you wanted

return this.role.equals(o.role);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249