• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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..
0
ms_lost
Asked:
ms_lost
  • 4
  • 2
  • 2
1 Solution
 
Peter KwanCommented:
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Peter KwanCommented:
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:
1. what is that Keyboard.class
2. you DID NOT TRIM the input. try using

name=Keyboard.readString().trim();
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now