String str=request.getParameter("input1");
24:      float f=0;
25:      try{
26:           f=Float.parseFloat(str);//Double.parseDouble(str)也有问题
27:         }
28:      catch(NumberFormatException e){
29:           out.print("error!");    
总是有这个问题,而我定义
String str=request.getParameter("input1");
24:      float f=0;
25:      try{
26:           f=Integer.parseInt(str);
27:         }
28:      catch(NumberFormatException e){
29:           out.print("error!");
    就没有问题,请问是什么原因?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【wongshiang】截止到2008-07-18 13:49:31的历史汇总数据(不包括此帖):
    发帖的总数量:7                        发帖的总分数:120                      每贴平均分数:17                       
    回帖的总数量:10                       得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:3                        结贴的总分数:40                       
    无满意结贴数:1                        无满意结贴分:20                       
    未结的帖子数:4                        未结的总分数:80                       
    结贴的百分比:42.86 %               结分的百分比:33.33 %                  
    无满意结贴率:33.33 %               无满意结分率:50.00 %                  
    楼主该结一些帖子了
      

  2.   

    做了个测试 发现没有异常 public static void main(String[] args) {
    String str ="5.983777";
    float a = Float.parseFloat(str);
    double b = Double.parseDouble(str);
    System.out.println(a);
    System.out.println(b);



    }你看看input1的内容到底是什么
      

  3.   

    把str打印出来              
      

  4.   

    String str=(String) request.getParameter("input1"); 
    不知道是不是跟没写(String)有关系.
      

  5.   

    把str输出来看看什么结果,这样就可以看到错误所在
      

  6.   

    <%@ page contentType="text/html;charset=GB2312"%>
    <HTML>
    <BODY BGCOLOR=cyan>
    <form name=form action="" method="post">
    <input type=text name=input1>
    <input type=submit name=submit value=提交>
    </form>
    <%!
        class Circle{
         double r;
         Circle(double r){
           this.r=r;
          }
         double 获取面积(){
           return Math.PI*r*r;
          }
         double 获取周长(){
           return Math.PI*2*r;
          }
       }
    %>
    <%
        String str=request.getParameter("input1");
         double d=0;
         try{
              d=Double.parseDouble(str);
            }
         catch(NumberFormatException e){
              out.print("error!");    
            }
    Circle circle=new Circle(d);
    %>
    <p>圆的面积是:
      <%=circle.获取面积()%>
    <BR>
    <p>圆的周长是:
      <%=circle.获取周长()%>
    <BR>
    </BODY>
    </HTML>
      

  7.   

    估计是String str=request.getParameter("input1"); 中str的值是个整数,而不是浮点数.可能没有小数点的数Float.parseFloat()方法会抛异常吧.
    没有打印出str的内容,这个无法确定,但是,根据楼主所提供的信息,应该是这种原因了 
      

  8.   

    没有进行空值判断,因为第一次运行的时候(没提交前)str是null,所以报异常取得str后进行空值判断即可。
    if(str == null)return;