Solved

isEmpty String of getParamString

Posted on 2009-07-15
7
320 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

717 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