Search code examples
javaeclipseuser-interfacetextboxverification

Validate an empty textbox?


I am currently coding a gui and want to verify that the user fills in the textbox a String. I tried that:

String name = textBoxes.get(0).getText();
if(name.isEmpty()) {
    showError("Pls insert a name!"); 
} else{
    name = textBoxes.get(0).getText();
}

But that really does not work? It seems to be that there is sth. "inside" the textbox, even if I do not fill sth. in.(I tried sysout and got that:

'                            '

So my question is, how to verify if a textbox is empty or not?


Solution

  • It sounds like your textBox is returning a string full of space characters. Try using name.trim().isEmpty() instead of just name.isEmpty() to remove those extra spaces before you do anything with it.

    trim() removes leading and trailing white spaces. See: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#trim%28%29