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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18535527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:52:00  来源:igfitidea点击:

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

javanumberformatexception

提问by Sandeep Armal

I am trying to run one program. I am really very newbie on java. When i run my program i am getting following exception..

我正在尝试运行一个程序。我真的是 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)    

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.

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

回答by Alex

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

It should variable firstnobut not a string "firstno".

它应该是变量firstno而不是字符串"firstno"

回答by Suresh Atta

You are passing String's Not the variables.

您正在传递 String 的 Not 变量。

 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 htmlcauses there type of exceptions.

我会建议,trim()传递之前的字符串,因为有时空格,来自html那里类型的原因exceptions.

回答by Fernando

You tried to convert a not-numeric string to a number.

您试图将非数字字符串转换为数字。

I believe you should do this

我相信你应该这样做

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

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

回答by Nicolas Rinaudo

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 firstnomight very well be.

您正在尝试解析字符串"firstno",而不是变量的内容firstno"firstno"不是合法整数,其中的内容firstno很可能是。

回答by Prashant Bhate

Use a variable name instead of a string

使用变量名而不是字符串

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

Few value adds to your code,

很少为您的代码增加价值,

  • Add validation to your code
  • Add Null check parameter values retrieved / set a default value / show error message when value is not passed
  • Handle NumberFormatExceptionand set a default value / show error message
  • Return Complete HTML document as indicated by content type
  • Use RequestDispatcherto switch between HTTP methods Post to get, or better use post redirect get pattern
  • 向您的代码添加验证
  • 添加检索到的空检查参数值/设置默认值/未传递值时显示错误消息
  • 处理NumberFormatException并设置默认值/显示错误消息
  • 返回由内容类型指示的完整 HTML 文档
  • 使用RequestDispatcher在 HTTP 方法之间切换 Post 获取,或者更好地使用 post 重定向获取模式

The Java EE Tutorialwould be a good starting point

Java EE的教程将是一个很好的起点