Solved Same string but Java doesn't see it?

Discussion in 'Plugin Development' started by Reteckz, Apr 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Reteckz

    Hello!

    I'm having a very weird problem.

    I am getting my first variable from a config by using:

    owner = protections.getString(count+".Owner");

    In the config this is:

    Owner: Reteckz

    I get the seconds string from:

    Player player = event.getPlayer();
    String name = player.getName();


    When I print both Strings in the console, they both say: Reteckz

    But when I use if(name==owner){ execute this } it doesn't execute it for some reason.

    So actually Java is saying here: Reteckz != Reteckz

    Anyone knows how to fix this? Maybe its because I got one String from a config and the other one from a player entity?
     
  2. Offline

    TGF

    Reteckz Could you give Your code pls?
     
  3. Offline

    Reteckz

    All the code parts that have something to do with this:


    Code:
    Player player = event.getPlayer();
    String name = player.getName();
     
    FileConfiguration protections;
    protections = getConfig();
    File protectionsFile = new File("plugins" + File.separator + "SpongeProtection" + File.separator + "protections.yml");
    protections = YamlConfiguration.loadConfiguration(protectionsFile);
     
    String owner;
    int checker;
     
    while(protections.contains(count)){
            checker = 0;
            owner = protections.getString(count+".Owner");
            if(owner==name){
                checker = 1;
            }else{
                            checker = 2;
                    }
    }

    And when I print checker to the console for example, it gives 2.
    And when I print the variables Owner & Name to the console, they both give Reteckz as result.
     
  4. Offline

    TGF

    Hmm, strange ;) Reteckz maybe owner.equalsIgnoreCase(name) ? or contains()
     
  5. Offline

    Reteckz

    Fixed it by creating an extra integer.

    int equal = owner.compareTo(name);

    For some reason this did work..
     
  6. Offline

    Tirelessly

    Just .equals to compare strings.
     
    macguy8 likes this.
  7. Offline

    Digi

    Reteckz
    The '==' logic comparators compares references or values of primitives.
    Use .equals() to compare values of objects, not just for Strings.

    The equalsIgnoreCase() method is String specific and checks strings without caring for letter casing, it's a bit slower than equals() because it does extra checks.
     
  8. Offline

    gomeow

    "hello" == "hello" : true
    new String("hello") == "hello" : false
    new String("hello") == new String("hello") : false
    new String("hello").equals(new String("hello")) : true
    new String("hello").equals("hello") : true
     
  9. Offline

    Cirno

    There's only an extreamly small chance that "this.string == that.string" will actually return true.
    Just as everyone has said above, use .equals, .equalsIgnoreCase, or you can be fancy and use Commons IO (I think somewhere along the lines in the code that's an improved comparison method for Strings)
     
Thread Status:
Not open for further replies.

Share This Page