| Author |
Changing String to Integer
|
Suman baul
Greenhorn
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
Greenhorn
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
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
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
Greenhorn
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
Greenhorn
Joined: Jul 16, 2012
Posts: 14
|
|
@Manjunath
@Campbell
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
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
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.
|
 |
 |
|
|
subject: Changing String to Integer
|
|
|