suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java返回java.lang.NumberFormatException异常

代码如下,其中getX()成功有返回结果,getY()却报异常.

public class AddressUtilDemo {
    public static void main(String[] args){
        AddressUtil AddressUtil = new AddressUtil();
        AddressUtil.setAddress("3454.234,24l.432");
        System.out.println(AddressUtil.getX());
        System.out.println(AddressUtil.getY());
    }

}
class AddressUtil {
    private String address;
    public AddressUtil(){}
    public AddressUtil(String address){
        if(address.indexOf(",")>0){
            this.address = address;
        }
    }
    public Double getX(){
        String string = address.substring(0, address.indexOf(",")) ;
        return Double.parseDouble(string);
    }
    public Double getY(){
        String string = address.substring((address.indexOf(",")+1));
        return Double.parseDouble(string);
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address.trim();
    }
}

试过string.trim()等方法都无效.debug看到的string确实是预期中的数值.
以下是异常信息:

Exception in thread "main" java.lang.NumberFormatException: For input string: "24l.432"
PHP中文网PHP中文网2804 Tage vor715

Antworte allen(3)Ich werde antworten

  • 阿神

    阿神2017-04-18 10:57:16

    你应该学会调试,看看getY中的string就知道问题了

    Antwort
    0
  • 阿神

    阿神2017-04-18 10:57:16

    24l.432
    24后们是 L,不是 1

    Antwort
    0
  • 高洛峰

    高洛峰2017-04-18 10:57:16

    骚年,你main函数里setAddress的是3454.234,24l.432(24L,是L不是1),所以y是错的,转化失败了,建议编辑器字体用Source Code Pro 还能看的出来

    Antwort
    0
  • StornierenAntwort