【发布时间】:2014-09-24 02:39:37
【问题描述】:
我正在尝试过滤用户的输入,以确保其最多为 4 位并且不是空字符串。无论我将其留空还是输入数字,!strInput.equals(null) 仍然是正确的。我是否错误地比较了字符串?
我也尝试过:!strInput.equals("")、strInput != null 和 strInput != "" 虽然我认为它应该是 .equals(...) 因为我正在尝试比较值。
private void updateFast() {
String strInput = JOptionPane.showInputDialog(null, "How many?");
if (!strInput.equals(null) && strInput.matches("\\d{0,4}"))
{
//do something
}
else
{
JOptionPane.showMessageDialog(null, "Error. Please Re-enter");
updateFast();
}
}
【问题讨论】:
-
用
strInput != null代替!strInput.equals(null)
标签: java string compare equals joptionpane