?
Solved

trouble converting string to integer

Posted on 2005-05-03
27
Medium Priority
?
311 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:rcmb
  • 10
  • 10
  • 6
26 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923319
Hi RCMB,

You can use equals method.

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

regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923324
You should use equals because Integer is not primitive.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923329
or alternatively,

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

Regards
Dave
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:aozarov
ID: 13923352
Dave
You are right, but why the && part?
Do just:
if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923355
Ok, you fixed it :-)
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923358
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923366
Hi aozarov,

Yes, I created a mistake :).

Regards
Dave
0
 
LVL 12

Author Comment

by:rcmb
ID: 13923370
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
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923381
>>  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.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923393
Printout the values before the comparision.
The latters options should work for you.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923404
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
0
 
LVL 12

Author Comment

by:rcmb
ID: 13923441
What does it return of grpTypeDTO.getGroup_TypeID()? Is it Integer or int?

Integer
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 600 total points
ID: 13923448
Hi ,

Then you can 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();
}

However, are you sure that grpTypeID and grpTypeDTO.getGroup_TypeID() have the same values?

Regards
Dave
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1400 total points
ID: 13923456
change:
if(grpTypeID.equals(grpTypeDTO.getGroup_TypeID())){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}
to (to avoid NullPointerException):
if(grpTypeDTO.getGroup_TypeID().equals(grpTypeID)){
    grpTypeName = grpTypeDTO.getGrpTypeName();
}

Also add before that statement:
System.out.println("Compared values: " + grpTypeDTO.getGroup_TypeID() + " -> " + grpTypeName);
to make sure that they are be equal.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923463
Use this printout statement instead (wrong cut/paste :-) :
System.out.println("Compared values: " + grpTypeDTO.getGroup_TypeID() + " -> " + grpTypeID);
0
 
LVL 12

Author Comment

by:rcmb
ID: 13923466
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923469
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
0
 
LVL 12

Author Comment

by:rcmb
ID: 13923478
Yee --- Hah

That fixed it. Thanks so very much.

RCMB
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923483
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
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923494
rcmb,
I think suprapto45 should also get points. don't you think so?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923516
aozarov,

thanks for your attention :).

Regards
Dave
0
 
LVL 12

Author Comment

by:rcmb
ID: 13923528
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
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923530
NP :-)
rcmb,
You can use this link http://www.experts-exchange.com/Community_Support/
to ask to re-open the question so you can give suprapto45 to points he deserves.
0
 
LVL 12

Author Comment

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

RCMB
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13923575
:-)
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13923598
Thanks RCMB and aozarov

I appreciate it :)

Regards
Dave
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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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