?
Solved

empty string

Posted on 2002-05-11
8
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 16

Expert Comment

by:Peter Kwan
ID: 7007565
wait.... is your application using GUI or just  command line?
0
 
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 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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