Solved

empty string

Posted on 2002-05-11
8
186 Views
Last Modified: 2010-03-31
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
Comment
Question by:ms_lost
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 7003953
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 7005429
do not forget to trim() the String

if (input == null || input.trim().length() ==0)
{
  // bad input
}
0
 

Author Comment

by:ms_lost
ID: 7007201
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
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 7007565
wait.... is your application using GUI or just  command line?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:heyhey_
ID: 7007804
post the relevant code
0
 

Author Comment

by:ms_lost
ID: 7012553
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
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 7013056
1. what is that Keyboard.class
2. you DID NOT TRIM the input. try using

name=Keyboard.readString().trim();
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7013058
>  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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now