Results 1 to 8 of 8
- 01-04-2010, 03:24 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
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;
and the output from the test console (javabeans, fwiw) is;@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()));
}
Is this not the right way to compare these strings? I know I shouldn't use == like C++ etc. Thanks for your input.Test no features valid and valid mac found with license file : test-no-features-valid-mac.lic
00242c3b2e24
00242c3b2e24
Test for equality: false
-
Could there be extra non-visualized characters present such as trailing spaces or new line? Have you tried trimming both Strings first?
- 01-04-2010, 03:38 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
No Fubarable, haven't tried that. Any suggestions on a suitable method?
- 01-04-2010, 03:45 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
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
- 01-04-2010, 03:45 PM #5
trim() and check.
Ramya:cool:
- 01-04-2010, 03:48 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
Thanks Ramya, trim() worked a treat. :)
- 01-04-2010, 11:25 PM #7
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 01-05-2010, 09:58 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
comparison of two (identical) strings doesn't work
By langaro in forum CLDC and MIDPReplies: 4Last Post: 10-02-2009, 04:02 PM -
Random Strings help!
By hhale in forum New To JavaReplies: 5Last Post: 09-23-2009, 12:57 PM -
Strings and delimeters
By Manfizy in forum New To JavaReplies: 12Last Post: 06-09-2009, 07:51 AM -
getting strings from arraylist
By Ms.Ranjan in forum New To JavaReplies: 15Last Post: 05-06-2009, 10:40 PM -
characters + strings
By Gilgamesh in forum New To JavaReplies: 3Last Post: 03-02-2008, 09:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks