Solved

empty string

Posted on 2002-05-11
8
188 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 54
jboss wildfly 10.1 10 81
Java Restore security prompts not working 10 8
Chrome and Firefox Java 5 27
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

786 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