java.lang.NumberFormatException:对于输入字符串:";firstno“;

java.lang.NumberFormatException:对于输入字符串:";firstno“;,java,numberformatexception,Java,Numberformatexception,我正在尝试运行一个程序。我真的是一个java新手。当我运行我的程序时,我得到以下异常 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

我正在尝试运行一个程序。我真的是一个java新手。当我运行我的程序时,我得到以下异常

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)    
这是我的代码供你参考

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");
 }

}
import java.sql.*;
导入java.io.*;
导入javax.servlet.*;
导入javax.servlet.http.*;
公共类MathEx扩展了HttpServlet
{
public void doGet(HttpServletRequest p,HttpServletResponse q)抛出ServletException,IOException
{
q、 setContentType(“文本/HTML”);
PrintWriter out=q.getWriter();
out.println(“”);
out.println(“输入第一个数字”);
out.println(“”);
out.println(“

”); out.println(“输入第二个编号”); out.println(“”); out.println(“

”); out.println(“”); out.println(“”); out.println(“”); out.println(“”); } public void doPost(HttpServletRequest s,HttpServletResponse t)抛出ServletException,IOException { t、 setContentType(“文本/HTML”); PrintWriter out=t.getWriter(); 字符串firstno=s.getParameter(“first”); 字符串secondno=s.getParameter(“Second”); 字符串选择=s.getParameter(“发送”); int fno=Integer.parseInt(“firstno”); int sno=Integer.parseInt(“secondno”); int结果; out.println(“第一个编号=”+fno); out.println(“

”); out.println(“第二个编号=”+sno); out.println(“

”); if(选择等于(“加法”)) { 结果=fno+sno; out.println(“加法结果=”+结果); } if(choice.equals(“减法”)) { 结果=fno-sno; out.println(“减法的结果=+结果); } if(选择等于(“结束”)) { out.println(“谢谢你,祝你有愉快的一天”); 返回; } out.println(“


”); 多吉特(s,t); { out.println(“


”); out.println(“再见”); } }
}

我真的不明白为什么会发生这种事。。请给我任何参考或提示

int fno=Integer.parseInt(firstno);
int sno=Integer.parseInt(secondno);

它应该是变量
firstno
,而不是字符串
“firstno”

您传递的字符串不是变量

 int fno=Integer.parseInt("firstno");
  int sno=Integer.parseInt("secondno");


我建议,在传递字符串之前,
trim()
字符串,因为有时空格来自
html
会导致
异常。
您尝试将非数字字符串转换为数字

我相信你应该这样做

      String firstno = s.getParameter("first");
      String secondno = s.getParameter("Second"); 

      int fno=Integer.parseInt(firstno);
      int sno=Integer.parseInt(secondno);

您试图解析的是字符串
“firstno”
,而不是变量
firstno
的内容。
“firstno”
不是一个合法的整数,
firstno
的内容很可能是这样的。

使用变量名而不是字符串

  int fno=Integer.parseInt(firstno);
  int sno=Integer.parseInt(secondno);
代码中没有多少附加值

  • 将验证添加到代码中
  • 添加检索到的空检查参数值/设置默认值/未传递值时显示错误消息
  • 处理
    NumberFormatException
    并设置默认值/显示错误消息
  • 按内容类型返回完整的HTML文档
  • 用于在HTTP方法之间切换Post to get,或者更好地使用Post重定向get模式
这将是一个很好的起点

  int fno=Integer.parseInt(firstno);
  int sno=Integer.parseInt(secondno);