java.lang.NumberFormatException: For input string: "firstno"

Sandeep Armal picture Sandeep Armal · Aug 30, 2013 · Viewed 19.3k times · Source

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.

Answer

Alex picture Alex · Aug 30, 2013
int fno=Integer.parseInt(firstno);
int sno=Integer.parseInt(secondno);

It should variable firstno but not a string "firstno".