Link to home
Start Free TrialLog in
Avatar of rcmb
rcmb

asked on

trouble converting string to integer

For some reason I cannot find a solution that works for this senario

Here goes

Integer grpTypeID = null;

//request.getParameter is passed from a form

if(request.getParameter("grpTypeID") != null){
   grpTypeID = new Integer(request.getParameter("grpTypeID"));
               //this is where I am confused -- I have tried Integer.parseInt but it does not work because grpTypeID is an
               //Integer not an Int
} else {
   grpTypeID = null;
}

Now this is where I cannot get a result -- e.g. grpTypeName is never returned

if(grpTypeID == grpTypeDTO.getGroup_TypeID()){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

I cannot get this statement to return a TRUE --

grpTypeDTO.getGroup_TypeID is an Integer

Any ideas on what I can do to get the if statement to return a TRUE if the values equal each other?

Thanks in advance,
RCMB
Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi RCMB,

You can use equals method.

if(grpTypeID == grpTypeDTO.getGroup_TypeID() && grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

regards
Dave
You should use equals because Integer is not primitive.

Regards
Dave
or alternatively,

if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

Regards
Dave
Avatar of aozarov
aozarov

Dave
You are right, but why the && part?
Do just:
if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}
Ok, you fixed it :-)
Hi,

If you want to use "==" then do it this way.

int grpTypeID; // Default to 0

if(request.getParameter("grpTypeID") != null){
   grpTypeID = new Integer(request.getParameter("grpTypeID").intValue());
} else {
   grpTypeID = 0;
}

Now this is where I cannot get a result -- e.g. grpTypeName is never returned

if(grpTypeID == grpTypeDTO.getGroup_TypeID()){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

I hope that helps.

Regards
Dave
Hi aozarov,

Yes, I created a mistake :).

Regards
Dave
Avatar of rcmb

ASKER

suprapto45

did not work -- no errors but still will not return a match.

Tried three methods
grpTypeID == grpTypeDTO.getGroup_TypeID() && grpTypeID.equals(grpTypeDTO.getGroup_TypeID()
grpTypeID == grpTypeDTO.getGroup_TypeID()
grpTypeID.equals(grpTypeDTO.getGroup_TypeID()

Still no luck
>>  grpTypeID = new Integer(request.getParameter("grpTypeID").intValue());
Or just do:
 grpTypeID = Integer.parseInt(request.getParameter("grpTypeID"));

Note, that in this case you will need to make grpTypeDTO.getGroup_TypeID() return int instead of Integer as well.
Printout the values before the comparision.
The latters options should work for you.
Hi,

What does it return of grpTypeDTO.getGroup_TypeID()? Is it Integer or int?

if it is Integer, then you can return TRUE if you have

Integer grpTypeID = null;

if(request.getParameter("grpTypeID") != null){
   grpTypeID = new Integer(request.getParameter("grpTypeID"));
} else {
   grpTypeID = null;
}

if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

if it is int and NOT Integer then you should have

int grpTypeID; // Default to 0

if(request.getParameter("grpTypeID") != null){
   grpTypeID = new Integer(request.getParameter("grpTypeID").intValue());
} else {
   grpTypeID = 0;
}

if(grpTypeID == grpTypeDTO.getGroup_TypeID()){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}


Regards
Dave
Avatar of rcmb

ASKER

What does it return of grpTypeDTO.getGroup_TypeID()? Is it Integer or int?

Integer
SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use this printout statement instead (wrong cut/paste :-) :
System.out.println("Compared values: " + grpTypeDTO.getGroup_TypeID() + " -> " + grpTypeID);
Avatar of rcmb

ASKER

Dave,

I tried the last suggestion you made (10:20PM) and here is the error:

22:21:34,243 ERROR [Engine] ApplicationDispatcher[/securityAdmin] Servlet.servic
e() for servlet jsp threw exception
java.lang.NullPointerException
        at org.apache.jsp.Administration.groupTypesMgmt_jsp._jspService(groupTyp
esMgmt_jsp.java:224)

This is line 224

if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){

RCMB
Mmm....

Then it means that your grpTypeID is null. Subsequently, it means that request.getParameter("grpTypeID") is also null. Is request.getParameter("grpTypeID") returning some values? (I don't think so)

Regards
Dave
Avatar of rcmb

ASKER

Yee --- Hah

That fixed it. Thanks so very much.

RCMB
If you want you can apply the change I suggested at (10:23PM) to avoid the NullPointer exception.
But the fact that it is null explain why the compare will fail :-)
request.getParameter("grpTypeID")  is case sensetive.
Make sure the sent html contains the right parameter name
rcmb,
I think suprapto45 should also get points. don't you think so?
aozarov,

thanks for your attention :).

Regards
Dave
Avatar of rcmb

ASKER

Okay -- I agree with you because he did a great deal of work. How can I do it now that I have awarded you points?

RCMB
NP :-)
rcmb,
You can use this link https://www.experts-exchange.com/Community_Support/
to ask to re-open the question so you can give suprapto45 to points he deserves.
Avatar of rcmb

ASKER

Done -- Once complete I will reassign the points -- Thanks for your help and taking the time to look out for other experts.

RCMB
:-)
Thanks RCMB and aozarov

I appreciate it :)

Regards
Dave