Results 1 to 8 of 8
  1. #1
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default Two identical strings are not identical...

    ...apparently. Of course I must be just doing something stupid. Hello all, I'm new to Java and new here too. Hope to get an answer to this;

    I've been learning java for a few weeks now and have been lucky enough to get an Internship position but I don't want to keep bothering my boss. I have tried unsuccessfully to figure out what I'm doing wrong but to no avail so here I am!

    I'm writing a simple unit test for this app and I want to compare two strings (two MAC addresses actually, one from the real user machine and one from a license file, to decide whether or not to run this app). I am sure the strings are the same, I even converted them to lower case to make sure I wasn't mistaking a digit 0 for a letter o etc. but it wasn't that simple. The code snippet is below;
    @Test
    public void testNoFeaturesValidAndValidMac() {
    String fileName = "test-no-features-valid-mac.lic";
    System.out.println("Test no features valid and valid mac found with license file : " + fileName);
    assertFalse(instance.checkForFeature("fileReader", new File(licenseFolder, fileName)));
    assertFalse(instance.checkForFeature("soundPlayer" , new File(licenseFolder, fileName)));
    System.out.println(/*"Mac found in license file is: "+*/instance.checkForMac("MacAddress1", new File(licenseFolder, fileName)).toLowerCase());
    System.out.println(/*"Actual machine mac is: " +*/instance.getActualMachineMac().toLowerCase());
    System.out.println("Test for equality: "+instance.checkForMac("MacAddress1", new File(licenseFolder, fileName)).equalsIgnoreCase(instance.getActualMach ineMac()));
    assertTrue(instance.checkForMac("MacAddress1", new File(licenseFolder, fileName)).equalsIgnoreCase(instance.getActualMach ineMac()));

    }
    and the output from the test console (javabeans, fwiw) is;
    Test no features valid and valid mac found with license file : test-no-features-valid-mac.lic
    00242c3b2e24
    00242c3b2e24
    Test for equality: false
    Is this not the right way to compare these strings? I know I shouldn't use == like C++ etc. Thanks for your input.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Could there be extra non-visualized characters present such as trailing spaces or new line? Have you tried trimming both Strings first?

  3. #3
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    No Fubarable, haven't tried that. Any suggestions on a suitable method?

  4. #4
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Ignore the last Q. Furable, you saved my life. I used the trim() function and sure enough there must have been trailing whitespace lurking in there. It's passing the test now. Big thumbs up to this site! :D

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    trim() and check.
    Ramya:cool:

  6. #6
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks Ramya, trim() worked a treat. :)

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    The next time, when printing such things, you should put ' before and after the string you print :) Than you'll notice things like this inmediatly
    I die a little on the inside...
    Every time I get shot.

  8. #8
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. comparison of two (identical) strings doesn't work
    By langaro in forum CLDC and MIDP
    Replies: 4
    Last Post: 10-02-2009, 04:02 PM
  2. Random Strings help!
    By hhale in forum New To Java
    Replies: 5
    Last Post: 09-23-2009, 12:57 PM
  3. Strings and delimeters
    By Manfizy in forum New To Java
    Replies: 12
    Last Post: 06-09-2009, 07:51 AM
  4. getting strings from arraylist
    By Ms.Ranjan in forum New To Java
    Replies: 15
    Last Post: 05-06-2009, 10:40 PM
  5. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 09:10 PM

Posting Permissions

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