Reputation: 67
I'm working on Library management project in which there is one column "available". I'm fetching value from it. if a value is "Yes" then only a new book will be issued. here is the code that I have tried.
try {
String a = checkStatus();
String b = "Yes";
System.out.println("a: "+a);
System.out.println("b: "+b);
if(a.equalsIgnoreCase(b))
{
System.out.println("working in if");
}
else
{
System.out.println("shoot! Fallout :(");
}
} catch (SQLException ex) {
Logger.getLogger(IssueBook.class.getName()).log(Level.SEVERE, null, ex);
}
Output:
a: Yes
b: Yes
shoot! Fallout :(
it should have return true instead of false. Where I'm making mistake? Thanks!
Upvotes: 0
Views: 106
Reputation: 338564
The Answer by rzwitserloot is very good. I would suggest using code point integers rather than char
. The char
type is legacy, and problematic.
String input = "Dog Cat" ;
int[] codePoints = input.codePoints().toArray() ;
for( int codePoint : codePoints )
{
System.out.println( "Code point " + codePoint + " is: " + Character.getName( codePoint ) ) ;
}
See this code run live at IdeOne.com. Notice the two different kinds of space characters in the middle.
Code point 68 is: LATIN CAPITAL LETTER D
Code point 111 is: LATIN SMALL LETTER O
Code point 103 is: LATIN SMALL LETTER G
Code point 32 is: SPACE
Code point 160 is: NO-BREAK SPACE
Code point 67 is: LATIN CAPITAL LETTER C
Code point 97 is: LATIN SMALL LETTER A
Code point 116 is: LATIN SMALL LETTER T
You can compare two int
arrays of code points.
System.out.println(
Arrays.equals(
"Dog Cat".codePoints().toArray() , // 2nd space-character is NO-BREAK SPACE.
"Dog Cat".codePoints().toArray() // 2nd space-character is SPACE.
)
);
false
Upvotes: 1
Reputation: 102902
Most likely those strings aren't actually equal. They contain a character that you can't see.
It's that or you aren't running the code you pasted, so it's a problem in your build setup.
For example, the string contains a byte order mark.
The way to figure this out is to go through each string and print char-by-char. Replace your 'shoot!' line with:
System.out.print("Not equal!\nA: ");
printPerChar(a);
System.out.println("B: ");
printPerChar(b);
....
void printPerChar(String in) {
System.out.print(in.length());
System.out.print(": ");
for (char c : in.toCharArray()) {
System.out.print(c + " (" + ((int) c) + ") ");
}
System.out.println();
}
And you'll see e.g. byte order marks, extra spaces, zero-length spaces, unicode characters that look exactly like 'Y', 'e', or 's', etcetera.
Upvotes: 2