亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java返回java.lang.NumberFormatException異常
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-04-18 10:55:16
0
3
742

代碼如下,其中getX()成功有返回結(jié)果,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確實是預期中的數(shù)值.
以下是異常信息:

Exception in thread "main" java.lang.NumberFormatException: For input string: "24l.432"
PHP中文網(wǎng)
PHP中文網(wǎng)

認證0級講師

reply all(3)
阿神

You should learn to debug, look at the string in getY and you will know the problem

阿神

24l.432
24 is L, not 1

小葫蘆

Sao Nian, the setAddress in your main function is 3454.234, 24l.432 (24L, which is L and not 1), so y is wrong and the conversion failed. It is recommended to use Source Code Pro as the editor font and you can still see it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template