Home > database >  Comparing values from database java
Comparing values from database java

Time:04-18

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!

CodePudding user response:

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.

  • Related