• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking if any input has been entered  RSS feed

 
Colin Swan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Could a person with a soft heart please help me out here? I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there. Your reply is much appreciated.

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if( empName.length() == 0 )
 
Colin Swan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The more recent versions of Java® have a String#isEmpty method, which does the same thing. You can also try
if (input.trim().isEmpty())...
and test whether the String is all whitespace according to the trim method.
 
Colin Swan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Campbell Ritchie! I am taking a Java course and found some valuable info here. Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit late, but wanted to chime in...

I like to use this when using Scanners and BufferedReaders and the like!



 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you ever get null from a Scanner? As far as I remember you can only get null if you use a Reader and you have reached the end of the file.
I do not believe Scanner#next ever returns null; if you have reached the end of the file, you will suffer an Exception. You can check for the existence of a particular kind of token with a Scanner using the hasNextXXX methods, but that works differently.
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can you ever get null from a Scanner? As far as I remember you can only get null if you use a Reader and you have reached the end of the file.
I do not believe Scanner#next ever returns null; if you have reached the end of the file, you will suffer an Exception. You can check for the existence of a particular kind of token with a Scanner using the hasNextXXX methods, but that works differently.


Quite right! Apologies!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!