jQuery in Action, 2nd edition*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing String to Integer" Watch "Changing String to Integer" New topic
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: 187
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: 39834
    
  28
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: 15
@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: 39834
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing String to Integer