我之前有效的 if 语句不再有效
My if statement which worked before no longer works
我正在尝试创建一个 if else 语句,一个接一个地检查多个值,看它们是否为 null。
我创建的 if else 语句之前工作得很好,但在重新启动我的电脑后它不再工作了。
感谢任何帮助。
这是我的代码:
//Declare Strings
String ProjectNo, Location, VP;
//Store values from text boxes into strings
ProjectNo = txtProjectNum.getText();
Location = txtLocation.getText();
VP = txtVPNo.getText();
if(ProjectNo == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (Location == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (VP == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else{
JOptionPane.showMessageDialog(null, "All fields have been filled in!");
}
我相当确定 getText
method never returns null
. If you want to check for an empty field, check .length() == 0
or .isEmpty
. You might consider .trim()
首先。例如:
if (projectNo.trim().isEmpty()) {
或者 trim
将文本输入变量时:
projectNo = txtProjectNum.getText().trim();
// ...
if (projectNo.isEmpty()) {
我正在尝试创建一个 if else 语句,一个接一个地检查多个值,看它们是否为 null。
我创建的 if else 语句之前工作得很好,但在重新启动我的电脑后它不再工作了。
感谢任何帮助。
这是我的代码:
//Declare Strings
String ProjectNo, Location, VP;
//Store values from text boxes into strings
ProjectNo = txtProjectNum.getText();
Location = txtLocation.getText();
VP = txtVPNo.getText();
if(ProjectNo == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (Location == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (VP == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else{
JOptionPane.showMessageDialog(null, "All fields have been filled in!");
}
我相当确定 getText
method never returns null
. If you want to check for an empty field, check .length() == 0
or .isEmpty
. You might consider .trim()
首先。例如:
if (projectNo.trim().isEmpty()) {
或者 trim
将文本输入变量时:
projectNo = txtProjectNum.getText().trim();
// ...
if (projectNo.isEmpty()) {