0

I'm a novice in Java. I want to remind my user if they didn't enter a String in the JTextField, so I use an if-else statement to safeguard. However, the reminder doesn't jump out when I test it.

How do I safeguard a string from being null in text field using if-else statement?

if ( !name == null && score >= 0) {
    Student stu = new Student(name, score);
    students.add(stu);
    }
}
else if(name == null) {
    reminder.setText("Could you tell me your name?");
}
else{
    reminder.setText("Enter numbers for score! Must be a positive number!");
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
yijia
  • 1

1 Answers1

2

Null checks are if (object != null) and if you want to check if a text field is filled,

JTextField field = new JTextField();
String text = field.getText();
if (text != null && text.length() > 0) {
    System.out.println("Field has data");
}
Joe
  • 1,316
  • 9
  • 17