2

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?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
maximus
  • 11,264
  • 30
  • 93
  • 124
  • 2
    You could also have a look at [Java Swing: Implementing a validity check of input values](http://stackoverflow.com/questions/12997742/java-swing-implementing-a-validy-check-of-input-values) – MadProgrammer Oct 23 '12 at 06:11

2 Answers2

4

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

Michael
  • 3,334
  • 20
  • 27
1

It really depends on which text box you are using. If you using the JTextField from javax.swing you'll want to use x.getText() to get the entire thing.

SyntaxRules
  • 2,056
  • 23
  • 32