Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

isEmpty String of getParamString

Posted on 2009-07-15
7
Medium Priority
?
327 Views
Last Modified: 2013-12-22
Hi

I have a java code needs to getParamString(parames, PARAM1);
However, sometimes users do not input age and then submit the form.

I use

int age =0;
if (getParamString(parames, PARAM1) != null || getParamString(parames, PARAM1) != "")
{
    age = Integer.parseInt(getParamString(parames, PARAM1));  
    //throws numberFormatException: null
}

How should I validate the param1 ?




int age = Integer.parseInt(getParamString(parames, PARAM1));

Open in new window

0
Comment
Question by:techques
7 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 24866044
This may *not* work as expected

if (getParamString(parames, PARAM1) != null ||
getParamString(parames, PARAM1) != "")


change it to

if (getParamString(parames, PARAM1) != null ||
getParamString(parames, PARAM1).length()>0)
0
 
LVL 92

Expert Comment

by:objects
ID: 24866048
if (getParamString(parames, PARAM1) != null && getParamString(parames, PARAM1).trim().length>0)
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 24866053
sorry typo there, and worth using a local var

String age = getParamString(parames, PARAM1);
if (age != null && age.trim().length()>0) {

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 24866057
you sttill need to catch the exception in case the user enters an invalid number

http://helpdesk.objects.com.au/java/how-to-convert-a-string-to-an-int
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 24866963
You don't want any spare space left in the variable, so:
String age = getParamString(parames, PARAM1);
if (age != null && (age = age.trim()).length()>0) {
    // Do it
}

Open in new window

0
 

Author Comment

by:techques
ID: 24867795
Yes,
the followings code do work, but not ||

Thanks for help

if (getParamString(parames, PARAM1) != null && getParamString(parames, PARAM1).trim().length>0)
 
String age = getParamString(parames, PARAM1);
if (age != null && age.trim().length()>0) {
 
String age = getParamString(parames, PARAM1);
if (age != null && (age = age.trim()).length()>0) {
    // Do it
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24868302
:-)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

824 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