Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Numberformat exception

hi i am getting NumberFormatException: For input string: "undefined" could you pls help me the code is as below
String status =request.getParameter("unchek");
String status1 =request.getParameter("unckekvalue");
if(status!=null){
                              int istat=Integer.parseInt(status);
                              }
if(status1!=null){
                              int istat1=Integer.parseInt(status1);
                              }
pls do help
0
surajguptha
Asked:
surajguptha
  • 5
  • 2
  • 2
  • +3
2 Solutions
 
objectsCommented:
you need to catch and handle the exception

int istat = -1;
try
{
    istat=Integer.parseInt(status);
}
catch (NumberFormatException ex)
{
   // not a number do whatever
}


do the same for istat1
0
 
surajgupthaAuthor Commented:
i am sorry.... its in jsp.... pls do consider..... thank you... how do i do it
0
 
objectsCommented:
why does that make a difference, you can still add try/catch black in your scriplet.

other option would to first check if value is "undefined":

if(status!=null && !status.equals("undefined"){
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TimYatesCommented:
>> For input string: "undefined"

is this in Javascript or JSP code?  Objects is right for JSP code...
0
 
bloodredsunCommented:
To expand on objects answers, here is an example of how you could use it in a JSP. In this example we are using a request parameter (the bit after the name of the jsp and the ? sign) as the value "status"

status.jsp
------------------
<html>
<head><title>formatted date example</title></head>
<body>
<%
int istat = -1;
String status = request.getParameter("status") ;//get status from request parameter

try      {
         istat=Integer.parseInt(status);
}
catch (NumberFormatException ex){
   istat = -1 ;
}
%>
If istat is -1 then the request parameter "status" is either null or not a number<br>
&nbsp;
<br>
istat = <%=istat%>
</body>
</html>
--------

if you go to  "http://localhost:8080/status.jsp" you output
----------------------------------
istat = -1

if you go to  "http://localhost:8080/status.jsp?status=2" you output
----------------------------------
istat = 2

if you go to  "http://localhost:8080/status.jsp?status=bert" you output
----------------------------------
istat = -1


So you can see that only when status is a valid number will it return anything other than -1 (you must also be careful in this example, as if the status parameter is -1, e.g. status/jsp?status=-1, then it will work but the output is -1!!)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

I like to avoid exception handling in JSPs when possible so I thought you might like this:

<%
String status = request.getParameter("unchek"), status1 = request.getParameter("unchekvalue");
int istat = -1, istat1 = -1;
if (status != null) {
    Number nstat = NumberFormat.getIntegerInstance().parse(status, new ParsePosition(0));
    istat = nstat == null ? -1 : nstat.intValue();
}
if (status1 != null) {
    Number nstat = NumberFormat.getIntegerInstance().parse(status, new ParsePosition(0));
    istat1 = nstat == null ? -1 : nstat.intValue();
}
%>

Remember to import java.text.NumberFormat and java.text.ParsePosition.

Regards,
Jim Cakalic
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Another possible alternative is to use JSTL tags:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<c:catch var="parsingError">
    <fmt:parseNumber var="nstat" value="${param.unchek}"/>
    <c:set var="istat"><% nstat.integerValue() %></c:set>
</c:catch>
<c:if test="${not empty parsingError}">
    <c:set var="istat" value="-1"/>
</c:if>
<c:set var="parsingError"/>
<c:catch var="parsingError">
    <fmt:parseNumber var="nstat" value="${param.unchekvalue}"/>
    <c:set var="istat1"><% nstat.integerValue() %></c:set>
</c:catch>
<c:if test="${not empty parsingError}">
    <c:set var="istat1" value="-1"/>
</c:if>

Mind you, I haven't tested this ... it's just off the cuff.

Jim
0
 
neonlinesProject managerCommented:
Change the code as follows and it will not throws any NumberFormatException

----------------------------------Cut Here-------------------------------------------
String status = "";
String status1 = "";

if (request.getParameter("unchek") != null)
    if (request.getParameter("unchek").trim().length() != 0)
          status  = request.getParameter("unchek").trim();

if (request.getParameter("unckekvalue") != null)
    if (request.getParameter("unckekvalue").trim().length() != 0)
          status1 = request.getParameter("unckekvalue").trim();

if(status.eqauls("") != true && status.eqauls("undefined") != true){
                              int istat=Integer.parseInt(status);
                              }
if(status1.eqauls("") != true && status1.eqauls("undefined") != true){
                              int istat1=Integer.parseInt(status1);
                              }

----------------------------------Cut Here-------------------------------------------


0
 
Jim CakalicSenior Developer/ArchitectCommented:
What if the user enters 'abc' in the field?
:-)
0
 
neonlinesProject managerCommented:
"undefined" is not user entered value.

The user should filter the String entries in javascript validation itself. Still if it is needed to be validated in Java code ... (!?) , U have to write some piece of code



0
 
Jim CakalicSenior Developer/ArchitectCommented:
At the risk of offending someone, I would venture to suggest a point split between objects and myself.

Jim
0
 
Jim CakalicSenior Developer/ArchitectCommented:
OK.
0

Featured Post

Technology Partners: 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!

  • 5
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now