Results 1 to 12 of 12
  1. #1
    ShedoSurashu is offline Member
    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0

    Default This is kinda diriving me nuts, the value I get from ini4j doesn't match the string

    So I'm using ini4J to parse ini files to my class. I have a string called version and even if the value that ini.get() retrieves is the exact same as the value of my variable, the result is still not true.

    Here's a minifed version of my code

    Java Code:
        String version = "1.0";
        File filePref = new File("preference.ini");
        Ini pref = new Ini();
        pref.load(filePref);
        if (pref.get("about","version") != version) {
             System.out.println(pref.get("about","version")); // returns 1.0
             System.out.println(version);                     // also returns 1.0
             MsgBox.error("You are using a mismatched preference file.", "Error");
        }
    It keeps firing my if statement but I can clearly see that both the System.out.println results are the exact same. I initially thought that 1.0 as getting converted to a float but even replacing the value to a complete string like "dog", it still won't match.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Use the equals() method to compare Strings, not the == or != operators.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ShedoSurashu is offline Member
    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Quote Originally Posted by Norm View Post
    Use the equals() method to compare Strings, not the == or != operators.
    Holy cow that did the trick! Thanks! I was just used to using == for comparing everything. Thanks! But how come == works sometime too?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    == is for primitives or to see if two object references point to the same object.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Quote Originally Posted by ShedoSurashu View Post
    Holy cow that did the trick! Thanks! I was just used to using == for comparing everything. Thanks! But how come == works sometime too?
    In java, == compares by reference not by value; however for the String =,== and + are overloaded and they work(built in) although operator overloading is not allowed.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    the String =,== and + are overloaded
    Not sure that is true.
    The assignment operator(=) assigns values to variables. How are Strings different from other variable types?
    The == compares the contents of the pointer, not what the pointer points to.
    The + is a concatenation operator for Strings. That could be considered another use (overload)
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Quote Originally Posted by Norm View Post
    Not sure that is true.
    The assignment operator(=) assigns values to variables. How are Strings different from other variable types?
    The == compares the contents of the pointer, not what the pointer points to.
    The + is a concatenation operator for Strings. That could be considered another use (overload)
    You can try it. It was explained to me last year by my professor.
    == now works for strings.

    yes = assigns values to variables, but in order to create a String object you would have to construct it first. new String("");

    If one indeed uses the string constructor, the == will not work.

    Java Code:
    	String s = "h";
    		String s1 = "h";
    		String s2 = "kk";
    		System.out.println(s==s1);
    		if (s == s1) {
    			System.out.println(s+s1+s2);
    		}
    output: hhkk

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Strange output from your execution of the posted code. I get:
    true
    hhkk
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Quote Originally Posted by Norm View Post
    Strange output from your execution of the posted code. I get:
    true
    hhkk
    Sorry, my bad, the same for me.. forgot to type true
    true
    hhkk

    Anyway, It is actually a scheme Java uses with strings. In brief, when you have a string "a" and you declare another string "a" and both have different references, they still point to the same object. It still comparison by reference though. but it is obvious they want to treat the string as a primitive

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    == does not always return true for two Strings when equals() does return true. String usage optimization is fooling you.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Yes, that's what I said in #7. The optimization indeed is what allows this to happen and it's bad IMO because it works in 'some' cases? this is not very clever is it?

    and don't get me wrong, I am not saying to use == for strings, I disagree with it even though I see many people do it right including some of my teachers.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: This is kinda diriving me nuts, the value I get from ini4j doesn't match the stri

    Because sometimes == does not work, it should never be used.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2013, 06:07 PM
  2. If string does not match a number of strings problem
    By Interista in forum New To Java
    Replies: 2
    Last Post: 11-17-2011, 04:49 PM
  3. Test if string *could* match regular expression
    By kjkrum in forum Advanced Java
    Replies: 11
    Last Post: 07-12-2011, 10:01 AM
  4. How to match arrayList with a String?
    By Lund01 in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2010, 02:07 PM
  5. String match to the linkedlist element
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-06-2009, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •