在数组中查找空字符串
已声明并初始化了字符串、名称数组。编写确定数组元素是否为空或引用空字符串所需的语句。如果任何元素为 null 或空,则将变量 hasEmpty 设置为 true,否则将其设置为 false。
hasEmpty=false;
for (int i=0;i<names.length;i++)
if (names[i].trim().equals("") || names[i]==null)
hasEmpty=true;
我的代码有什么问题吗?
An array of Strings, names, has been declared and initialized. Write the statements needed to determine whether any of the the array elements are null or refer to the empty String. Set the variable hasEmpty to true if any elements are null or empty-- otherwise set it to false.
hasEmpty=false;
for (int i=0;i<names.length;i++)
if (names[i].trim().equals("") || names[i]==null)
hasEmpty=true;
Whats wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果数组的成员为
null
,首先调用trim()
将导致 NullPointerException。颠倒条件的顺序 -||
的短路性质将确保仅在真实的 String 对象上调用trim
。Calling
trim()
first will result in a NullPointerException should a member of the array benull
. Reverse the order of the conditions - the short-circuiting nature of||
will then ensure thattrim
is only called on a real String object.考虑一下names[i].trim()。
当
names[i]
是一个字符串时,你确实有像someString.trim()
这样的东西,它工作得很好。然而,当
names[i]
为 null 时,您确实拥有类似null.trim()
的东西。您已经发现 null 不允许使用 trim() 方法。 (事实上,我什至不太确定“null”是什么。)因此,在调用trim() 之前必须检查null。
当你有
a && b
,其中a
和b
是表达式,检查从左到右进行,一旦问题解决,解析器就会停止。因此,对于逻辑与运算符 (&&),如果a
为 false,则永远不会检查b
。这就是允许工作的原因。如果
a
为 null,则 a.trim() 部分不会执行,因为从逻辑角度来看它毫无意义;条件的值已经确定。类似地,
如果
a
为 null,则永远不会执行 a.trim() 部分,并且我们不会收到错误。Consider
names[i].trim()
.When
names[i]
is a String, you really have something likesomeString.trim()
which works fine.When
names[i]
is a null, however, you really have something likenull.trim()
. You've already discovered that null doesn't allow a trim() method. (In fact, I'm not even really sure what 'null' is.)Therefore, you must check for null before you invoke trim().
When you have
a && b
, wherea
andb
are expressions, the checks are made left-to-right and the parser stops as soon as the issue is settled. So for the logical and operator (&&), ifa
is false thenb
is never checked. This is what allowsto work. if
a
is null, the a.trim() part is not executed since it would be pointless from a logical point of view; the value of the conditional has been decided.Similarly for
if
a
is null then the a.trim() part is never performed and we don't get an error.您可以使用 Apache Commons Lang 的 isBlank() 检查字符串:
StringUtils.isBlank
正在检查字符串是否为null
或空(即是否等于删除所有空白字符后变为""
)。You can use the Apache Commons Lang's isBlank() to check a String:
StringUtils.isBlank
is checking if the String isnull
or empty (i.e. if it is equals to""
when all blank characters are removed).它抛出一个空指针异常,因为你试图在一个空对象上运行一个方法:
所以,只要names[]有一个空的名称,它就会抛出异常。解决该问题的一种方法是切换 if 语句中的布尔语句:
It's throwing a Null Pointer Exception because you're trying to run a method on a null object:
So, anytime that names[] has ONE name that's null, it will throw the exception. One way to solve the problem is to switch the boolean statements in this if statement: