Solved

isEmpty String of getParamString

Posted on 2009-07-15
7
319 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 73
login jsp example 24 101
ejb mdb examples 1 35
collection output issue 9 88
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

733 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