Solved

isEmpty String of getParamString

Posted on 2009-07-15
7
318 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 50 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
java jdbc batch example 8 41
hibernate example issues from command prompt 10 50
Glassfish admin console not working 1 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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