2013-08-30 77 views
2

我想运行一个程序。我在java上真的很新鲜。当我运行我的程序我越来越下面的例外..java.lang.NumberFormatException:对于输入字符串:“firstno”

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

} 

}

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

回答

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

它应该变量firstno,但不是一个字符串"firstno"

1

您正在传递String的不是变量。

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

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

而且我建议,trim()传递之前,将字符串,因为有时空格,从html来使有你试图转换的exceptions.

0

类型一个数字的非数字字符串。

我相信你应该这样做

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

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

你试图解析字符串"firstno",变量firstno的不是内容。 "firstno"不是合法的整数,其中firstno的内容可能非常合适。

0

使用一个变量名而不是字符串

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

几个值添加到您的代码,

  • 添加验证代码
  • 添加空校验参数值获取/设置默认值/显示错误消息当值不通过
  • 处理NumberFormatException并设置一个默认值/显示错误消息
  • 返回完整的HTML文档的内容类型
  • 使用RequestDispatcher到HTTP方法邮政之间进行切换来获得,或更好地利用重定向后得到的图案

所指出的。Java EE Tutorial将是一个很好的起点

相关问题