Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing values from database java

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

b: Yes

shoot! Fallout 🙁

it should have return true instead of false. Where I’m making mistake?
Thanks!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading