0

I tried to use counter to count the amount of null or empty string. But it always failed. I print them to test it.

System.out.println(samples.get(0).getAuthor2());
System.out.println(samples.get(0).getAuthor2() == " ");
System.out.println(samples.get(0).getAuthor2().isEmpty());
System.out.println(samples.get(0).getAuthor2() == null);

The result is like this. screenshot If these are all false, what is this blank?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Yulia
  • 27
  • 4
  • 2
    You should check `.trim().isEmpty()`. Note that `==` is not how you compare strings. Is `.trim().isEmpty()` also false? – Sweeper May 22 '20 at 07:03
  • why would the first statement be false? You just print the string itself which consists of most likely of a space. If you want to know exactly print out the ascii code of the string – Amir Schnell May 22 '20 at 07:03
  • An empty string is a string that has no characters, not a string that consists of blanks. – DYZ May 22 '20 at 07:04
  • Try `System.out.println(samples.get(0).getAuthor2().equals(" "))`. – MC Emperor May 22 '20 at 07:07
  • Alright. Thank guys. I made a stupid mistake. == can not compare the strings and my editor didn't report it. I debugged so many times and I didn't find it. Sorry I'm goofy. – Yulia May 22 '20 at 07:11
  • @Yulia actually, == does compare objects, which is why your editor won't mention it, but it compares the referenced objects, not their values. The IDE can't know whether you want to compare values or references – Stultuske May 22 '20 at 07:35

4 Answers4

0

Try to use org.apache.commons.lang3.StringUtils#isBlank(string)

  • this should be a comment, not an answer – Stultuske May 22 '20 at 07:08
  • Sorry, but I can't comments because of low rank(( – Roman Yushchenko May 22 '20 at 07:12
  • There's a reason for that. By getting enough rep, you (must) have shown to post insightful answers. This limits the chances of bad commenteers being able to post comments. Unfortunately, if you use answers to post comments, you risk being downvoted, which means it'll take even longer for your rep to be high enough to comment. – Stultuske May 22 '20 at 07:26
  • @Stultuske, Thx for the explanation. The next time I'm careful in writing a response. – Roman Yushchenko May 22 '20 at 07:33
0

To test String you have to use equals method so you have

System.out.println(samples.get(0).getAuthor2().equals(" "));

to be true

Francesco Genta
  • 333
  • 1
  • 8
0

The first println statement outputs the value of getAuthor2() which is a space character (" "). This is not an empty string. It does not evaluate to a boolean (true/false) because it's a String object. The other lines evaluate to boolean because they are printing the result of comparators and not the string itself.

The seconds println is not how you compare strings in Java. It compares references to two different strings that both happen to have a space character in them, but they are different references. To see if getAuthor2() is a single space you should use the .equals function like this and you should get true:

System.out.println(samples.get(0).getAuthor2().equals(" "));

The third println also gives false because it's not empty. It has a space in it.

Similarly the fourth println also gives false because it's not null either.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
0
System.out.println(samples.get(0).getAuthor2() == " "); // This is condition , either true or false

System.out.println(samples.get(0).getAuthor2().isEmpty()); // This is condition ,isEmpty method return boolean either true or false

System.out.println(samples.get(0).getAuthor2() == null);// This is condition , either true or false

But

System.out.println(samples.get(0).getAuthor2()); // here you are printing the state of getAuthor2(), which is not a boolean condition ,so blank
Sachin Kumar
  • 1,055
  • 9
  • 15