Solved

isEmpty String of getParamString

Posted on 2009-07-15
7
314 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
Comment Utility
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
Comment Utility
if (getParamString(parames, PARAM1) != null && getParamString(parames, PARAM1).trim().length>0)
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
sorry typo there, and worth using a local var

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

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 92

Expert Comment

by:objects
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

10 Experts available now in Live!

Get 1:1 Help Now