File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Changing String to Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing String to Integer" Watch "Changing String to Integer" New topic

Changing String to Integer

Suman baul

Joined: Jul 27, 2012
Posts: 1
Hi Im Comletely new to jsp and was developing a registration page.
My problem is that i want to change the last to variables into integer as mysql tables having integer value

String gname=request.getParameter("gname");
String add1=request.getParameter("add1");
String add2=request.getParameter("add2");
String city=request.getParameter("city");
String state=request.getParameter("state");
String mob1=request.getParameter("mob1");
String mob2=request.getParameter("mob2");

Please Help..
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
int mob1 = Integer.parseInt(request.getParameter("mob1"));
Manjunath Gajula

Joined: Dec 21, 2011
Posts: 12
Its better to have null check before you parse

if(null != request.getParameter("mob1")) {
int mob1 = Integer.parseInt(request.getParameter("mob1"));
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37923
The idiom if (null != ... is a specific C/C++ idiom. We are writing Java, so can write if (obj != null) ...
But I disagree about the non-null check. I would suggest that in the event of a null getting into your application, you are better off throwing an Exception. If a null does occur, what value are you going to insert into the database?
Manjunath Gajula

Joined: Dec 21, 2011
Posts: 12
Hi Campbell Ritchie,
Thanks for your comment. yes i agree and it can be obj != null.
However, having a null check would be better is what i think. All the fields might not be mandatory and having a null check would save us there.

I welcome your comments.
Branden Bobo

Joined: Jul 16, 2012
Posts: 15

Hello Campbell and Manjunath

I agree you both but could he not Make those required fields i.e.
while(obj == null)
prompt("enter obj");

or is this what you suggesting?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37923
It all depends on the design of your application. There are some cases where you may permit nulls, eg in middle names. There are some cases where nulls would be dangerous, so an exception is warranted. There are some cases where the user will know the data, and can supply the information from a prompt. There are some instances where the user may not know those data, and there is a risk of their entering wrong data.

All those suggestions are therefore correct, in different circumstances.
It is sorta covered in the JavaRanch Style Guide.
subject: Changing String to Integer
Similar Threads
select in hibernate
Ajax code getting null value
JList help.