i / p字符串出错

时间:2011-04-07 03:18:33

标签: java

在我的项目中,我有一个主窗口,其中我已经采取了更新按钮但是当我更新数据时它显示错误,即错误:输入字符串:“2345678.0”

//整个o / p

init:
Deleting: C:\Documents and Settings\amit bidwai\sanskar1\build\built-jar.properties
deps-jar:
Updating property file: C:\Documents and Settings\amit bidwai\sanskar1\build\built-jar.properties
compile:
run:

 Driver loaded
Hi
SeniorPerson: cvbxc
RegistrationNo: 7
NativePlace: bvb
Kul: bvb
Gotra: bvb
KulSwami: bv
ResidensialAddress: fdgd
PinCode: 6564
STDcode: null
TelephoneNo: 5435435.0
MobileNo: 5435435
Email: fdsfsd@
Website: hgfhgfh
Education: hgfh
Branch: hgfh
BloodGroup: A+ve

 Driver loaded
Hi
SeniorPerson: arya
RegistrationNo: 25
NativePlace: hgh
Kul: hgh
Gotra: hgh
KulSwami: hgh
ResidensialAddress: hghg
PinCode: 2344356
STDcode: 4343
TelephoneNo: 2345678.0
MobileNo: 1234567891
Email: dsa@
Website: www
Education: hgfhh
Branch: ghfh
BloodGroup: O-ve

 Driver loaded
statement is created
error:For input string: "2345678.0"

我的代码是:

String regno1= cbregn.getSelectedItem().toString();
          if(regno1.equals("")){
          JOptionPane.showMessageDialog(null," ENTER THE REGISTRATION NO ");
 return;
        }

         try
         {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          System.out.println("\n Driver loaded");
          Connection con=DriverManager.getConnection("jdbc:odbc:wanisamajDB");
          Statement stmt=con.createStatement();
          System.out.println("statement is created");

          String qry= " UPDATE Registration1 set RegistrationNo = '"+cbregn.getSelectedItem().toString()+"',SeniorPerson = '"+cbnm.getSelectedItem().toString()+"', NativePlace = '"+tfplace.getText()+"', Kul = '"+tfkul.getText()+"', Gotra = '"+tfgotra.getText()+"' ,KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+Integer.parseInt(tfpcd.getText())+"', STDcode = '"+Integer.parseInt(tfstdcode.getText())+"', TelephoneNo = '"+Integer.parseInt(tftele.getText())+"', MobileNo = '"+Integer.parseInt(tfmno.getText())+"', Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem().toString()+"' where RegistrationNo='" +Integer.parseInt(cbregn.getSelectedItem().toString())+"'" ;
          String qry1= " UPDATE Registration1 set RegistrationNo = '"+cbregn.getSelectedItem().toString()+"',SeniorPerson = '"+cbnm.getSelectedItem().toString()+"', NativePlace = '"+tfplace.getText()+"', Kul = '"+tfkul.getText()+"', Gotra = '"+tfgotra.getText()+"' ,KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+Integer.parseInt(tfpcd.getText())+"', STDcode = '"+Integer.parseInt(tfstdcode.getText())+"', TelephoneNo = '"+Integer.parseInt(tftele.getText())+"', MobileNo = '"+Integer.parseInt(tfmno.getText())+"', Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem().toString()+"' where SeniorPerson='" +cbnm.getSelectedItem().toString()+"'" ;
          stmt.executeUpdate(qry);
          stmt.executeUpdate(qry1);
          JOptionPane.showMessageDialog(null,"RECORD IS UPDATED SUCCESSFULLY ");
          System.out.println("QUERY");       

          cbregn.setEditable(false);
          cbnm.setEditable(false);
          tfplace.setEditable(false);
          tfkul.setEditable(false);
          tfgotra.setEditable(false);
          tfswami.setEditable(false);
          taraddr.setEditable(false);
          tfpcd.setEditable(false);
          tfstdcode.setEditable(false);
          tftele.setEditable(false);
          tfmno.setEditable(false);
          tfemail.setEditable(false);
          tfweb.setEditable(false);
          tfedu.setEditable(false);
          tfbrch.setEditable(false);
          cbbldgrp.setEditable(false);
          con.close();
          stmt.close();
        }
        catch(SQLException eM)
        {
        JOptionPane.showMessageDialog(null,"RECORD IS NOT FOUND ");
        }
        catch(Exception et)
        {
        System.out.println("error:"+et.getMessage());
        }  

2 个答案:

答案 0 :(得分:1)

TelephoneNo: 5435435.0
TelephoneNo: 2345678.0

您正尝试将可以表示为Stringdouble的{​​{1}}转换为float。如果字符串不包含可解析的整数,Integer.parseInt()方法将抛出java.lang.NumberFormatException

所以

int

应该是

Integer.parseInt(tftele.getText())

Double.parseDouble(tftele.getText())

答案 1 :(得分:1)

由于您提供问题的详细信息是查看您正在通过的日期。由于您的代码对手机号码/电话号码要求很高,因此它永远不会是浮动或双重类型。它的类型应为int或long。

  

error:For input string: "2345678.0"

传递没有小数点的数据然后你的代码应该工作。在其他情况下,如果你想处理这种情况,那么你的两个解决方案

  1. 只需使用字符串方法从手机号码中删除小数点后的数字,然后将其转换为整数数据类型。
  2. 如果你不想这样,那么将传递的字符串转换为float / double类型,然后输入cast into int type。
  3.   

    有关类型转换和强制转换的参考,请http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html