• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException  RSS feed

 
zeb usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends I have just started learning Java.I am getting a problem may be a small one but i coundn't able to figure it out.so here is my Servlet code iam trying to insert data into database after submitting form iam getting java.lang.NumberFormatException: For input string: ""
I want to know how to check user entered String instead of Int ??


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Integer.parseInt to check if a string contains a number then you need to catch that exception as part of your error checking. An empty string is not a valid number - that's why you get the exception.

Having said that, storing a phone number in an int variable is a bad idea - most phone numbers start with a zero and this will be lost when you store the value in an int. Plus there could also be () or - in the number as well. Far better to store phone numbers in a String.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want only integer values to be inserted. There are two ways available :
1.You can do a simple java script validation in the UI. Check that field should not be empty and must contain integer.It is a simple thing to do.
2.In the server side you can do following validations

plus you can also check that the "mobile" variable contains integers or not. If yes, then only parse it to integer which you are doing in the very first step.
 
zeb usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Your reply.
How can i insert date into database using dropdown like facebook ???
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"zeb usman", please check your private messages for an important administrative matter. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!