d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > String Matching Problem > Code Debug
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Oct 10 2012 10:41pm
It's late, so hopefully im just being stupid, but this String problem is frustrating me big time. Basically I am comparing two strings that look and should be identical, but when i compare them, they dont equal each other. The compare1 string is originating from an excel spreadsheet and is the converted to a String, compare2 is set to "NoURL". When I hit the test loops, it prints 01234 for both compare1 and compare2, but then it also prints "Different status". The commented code also prints out 01234 when comparing the two strings char by char. I fell like I am missing something fundamental here...

Code

String compare1 = ((SidneyItem)sidneyItems.elementAt(j)).getOldAvailability().trim();
String compare2 = ((SidneyItem)sidneyItems.elementAt(j)).getAvailability().trim();

System.out.println(compare1);  //NoURL
System.out.println(compare2);  //NoURL
// Testing----------------------------------------------------------------
if(compare1 != "")
{
if(compare1.charAt(0) == 'N') System.out.print("0");
if(compare1.charAt(1) == 'o') System.out.print("1");
if(compare1.charAt(2) == 'U') System.out.print("2");
if(compare1.charAt(3) == 'R') System.out.print("3");
if(compare1.charAt(4) == 'L') System.out.print("4");
//System.out.print(compare1.charAt(5)); //Fails because out of range. ie. no white space at the end
}
/*if(compare1 != "")
{
       if(compare1.charAt(0) == compare2.charAt(0)) System.out.print("0");
if(compare1.charAt(1) == compare2.charAt(1)) System.out.print("1");
if(compare1.charAt(2) == compare2.charAt(2)) System.out.print("2");
if(compare1.charAt(3) == compare2.charAt(3)) System.out.print("3");
if(compare1.charAt(4) == compare2.charAt(4)) System.out.print("4");
}*/
//---END TESTING-------------------------------------------------------
if(compare1.trim() != compare2.trim())   //"NoURL" != "NoURL"?
{
System.out.println("Different status.\n");
}


Any thoughts?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 10 2012 10:47pm
Quote
To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality. Similarly, use the .compareTo() method to test for unequal comparisons. For example,
    String s = "something", t = "maybe something else";
    if (s == t)      // Legal, but usually WRONG.
    if (s.equals(t)) // RIGHT
    if (s > t)    // ILLEGAL
    if (s.compareTo(t) > 0) // CORRECT>


found that on some java site. I am assuming that != falls under the same case as ==, which as noted above, won't work.
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Oct 10 2012 10:53pm
Thanks a bunch, that works perfectly now.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 10 2012 10:54pm
Quote (xandumx @ Oct 10 2012 11:53pm)
Thanks a bunch, that works perfectly now.


you're welcome. Just for future reference, I am relatively new to programming and I have never seriously programmed in Java. I just went to google and typed "Java string comparison" to find that answer.

Surprisingly, google is pretty nice to programmers looking for accurate information. We pretty much run the internet, after all.....


edit: Just saying that because these forums are relatively slow when it comes to answers (usually) and I know the feeling of being stuck sometimes is a pain in the ass.

I do love learning about stuff though and Java is actually the main focus of the next course I am taking at college so a little preliminary research can't hurt on my end ;)

This post was edited by Eep on Oct 10 2012 10:56pm
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Oct 10 2012 11:05pm
I was thinking it was something to do with my excel file, not the string comparison itself. I didn't start with java, so when I was taught at school it was mostly just used to show OOP, not any details. Just picking the details up through practice now.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 10 2012 11:13pm
Quote (xandumx @ Oct 11 2012 12:05am)
I was thinking it was something to do with my excel file, not the string comparison itself.  I didn't start with java, so when I was taught at school it was mostly just used to show OOP, not any details.  Just picking the details up through practice now.


ahh.

That is usually the thing that gets everyone. At least for me. I always assume it is one thing but it ends up being another.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll