empty string

hi..
how to validate the user's input to not accept an empty string

 i used "" and null and it didn't stop him..
ms_lostAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
heyhey_Connect With a Mentor Commented:
1. what is that Keyboard.class
2. you DID NOT TRIM the input. try using

name=Keyboard.readString().trim();
0
 
Peter KwanAnalyst ProgrammerCommented:
Either check whether:

input.equals("") or input.length() == 0

holds. If the condition holds, then the user inputs an empty string, and execute your invalid code.

Here is a simple sample code:

if (input.equals("")) {      // or input.length==0, they are the same
      // do whatever invalid code here
}
else {
       // do whatever valid code here
}
0
 
heyhey_Commented:
do not forget to trim() the String

if (input == null || input.trim().length() ==0)
{
  // bad input
}
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ms_lostAuthor Commented:
ok i did what u've said.. but when i'm not entering anything and i press enter..it should show the error message..but it's not and i press and press and get nothing!!!!!!!!!!!!!
0
 
Peter KwanAnalyst ProgrammerCommented:
wait.... is your application using GUI or just  command line?
0
 
heyhey_Commented:
post the relevant code
0
 
ms_lostAuthor Commented:
i don't know what to do..here is the code im doing..

System.out.println("Enter the name (or press 'Enter' to quit):");
name=Keyboard.readString();
while(name.length()!=0)
{
if it's valid do other things..
}
the end of program..  // yeah i have question.. what is the code to end a program is it system.exit or what..

plz try to help......
0
 
heyhey_Commented:
>  what is the code to end a program is it system.exit
or what..

If there is only one thread (command line application, that does not use Threads), all you have to do is return from main() method. Otherwise you will have to call System.exit()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.