java.lang.NumberFormatException: For input string: "firstno"
19,690
Solution 1
int fno=Integer.parseInt(firstno);
int sno=Integer.parseInt(secondno);
It should variable firstno
but not a string "firstno"
.
Solution 2
You're attempting to parse the string "firstno"
, not the content of the variable firstno
.
"firstno"
is not a legal integer, where the content of firstno
might very well be.
Solution 3
You are passing String's Not the variables.
int fno=Integer.parseInt("firstno");
int sno=Integer.parseInt("secondno");
to
int fno=Integer.parseInt(firstno);
int sno=Integer.parseInt(secondno);
And I would suggest, trim()
the String before passing it,Because sometimes the white spaces ,came from the html
causes there type of exceptions.
Author by
Admin
Updated on June 14, 2022Comments
-
Admin almost 2 years
I am trying to run one program. I am really very newbie on java. When i run my program i am getting following exception..
type Exception report message For input string: "firstno" description The server encountered an internal error that prevented it from fulfilling his request. exception java.lang.NumberFormatException: For input string: "firstno" java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) java.lang.Integer.parseInt(Integer.java:492) java.lang.Integer.parseInt(Integer.java:527) MathEx.doPost(MathEx.java:34) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Here is my code for your reference.
import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MathEx extends HttpServlet { public void doGet(HttpServletRequest p, HttpServletResponse q) throws ServletException, IOException { q.setContentType("Text/HTML"); PrintWriter out = q.getWriter(); out.println("<form method=post>"); out.println("Enter first number"); out.println("<input type=text name=first>"); out.println("<br><br>"); out.println("Enter second no."); out.println("<input type=text name=second>"); out.println("<br><br>"); out.println("<input type=submit name=send value=ADDITION>"); out.println("<input type=submit name=send value=SUBSTRACTION>"); out.println("<input type=submit name=send value=END>"); out.println("</form>"); } public void doPost(HttpServletRequest s, HttpServletResponse t) throws ServletException, IOException { t.setContentType("TEXT/HTML"); PrintWriter out=t.getWriter(); String firstno = s.getParameter("first"); String secondno = s.getParameter("Second"); String choice = s.getParameter("send"); int fno=Integer.parseInt("firstno"); int sno=Integer.parseInt("secondno"); int result; out.println("First no ="+fno); out.println("<br><br>"); out.println("Second no ="+sno); out.println("<br><br>"); if (choice.equals("ADDITION")) { result=fno+sno; out.println("The result of addition= "+result); } if (choice.equals("SUBSTRACTION")) { result=fno-sno; out.println("The result of substraction= "+result); } if (choice.equals("END")) { out.println("Thank you have a nice day"); return; } out.println("<br><br><br>"); doGet(s,t); { out.println("<br><br><br>"); out.println("bye bye"); } }
}
I really don't understand why happening this.. Please give me any reference or hint.