gpt4 book ai didi

java - 从 CSV 解析 Double 会抛出 RuntimeException

转载 作者:行者123 更新时间:2023-12-02 10:47:39 25 4
gpt4 key购买 nike

我正在尝试从 CSV 文件读取数据。一切正常,但是当我尝试读取经度和纬度数据时。它给了我以下错误消息。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yogi.guestlogix/com.example.yogi.guestlogix.MainActivity}: java.lang.NumberFormatException: Invalid double: "EVE"
Caused by: java.lang.NumberFormatException: Invalid double: "EVE"**

MainActivity.java

   //Airports data begin
InputStream isss = getResources().openRawResource(R.raw.airports);
BufferedReader readerss = new BufferedReader(
new InputStreamReader(isss, Charset.forName("UTF-8"))
);

String liness = "";
//for airports.csv
try {
readerss.readLine();
while ((liness = readerss.readLine()) != null) {
//split by ',' first
String[] airport = liness.split(",");

//Read the data
AirportsData airdata = new AirportsData();
airdata.setAirportname(airport[0]);
airdata.setAirportcity(airport[1]);
airdata.setAirportcountry(airport[2]);
airdata.setAirportIATAcode(airport[3]);
//airdata.setAirportlang(Double.parseDouble(airport[4]));
//airdata.setAirportlat(Double.parseDouble(airport[5]));
AirportsDatas.add(airdata);
}
} catch (IOException e) {
Log.wtf("My Activity", "Reading data file error " + liness, e);
e.printStackTrace();
}


Log.d("Airline", "name is " + AirportsDatas);

AirportsData.java

public double getAirportlang(double airportlang) {
return airportlang;
}


public void setAirportlang(double airportlang) {
this.airportlang = airportlang;
}

public double getAirportlat( ) {
return airportlat;
}

public void setAirportlat(double airportlat) {
this.airportlat = airportlat;
}

@Override
public String toString() {
return "AirportsData{" +
"airportname='" + airportname + '\'' +
", airportcity='" + airportcity + '\'' +
", airportcountry='" + airportcountry + '\'' +
", airportIATAcode='" + airportIATAcode + '\'' +
", airportlang=" + airportlang +
", airportlat=" + airportlat +
'}';
}

**任何针对此问题的解决方案将不胜感激....

最佳答案

CSV 文件的问题是,用户(和其他程序员)几乎可以在文件中写入他们想要的内容。

就您的情况而言,有人在 CSV 单元格中输入了一个非数字,而您原本希望在该单元格中输入 double float 。 Java 正确地提示它无法解析为 double 。您有两种方法来处理这种情况:

A) 按契约(Contract)设计

首先,您可以通过依赖design by contract来解决这个问题。您可以有一个 parseLat 方法:

public Double parseLat(String[] csvRow) {
final String lat= csvRow[4];
assertIsNumeric(lat);
}

private void assertIsNumeric(String lat) {
if(!isNumeric(lat)) {
throw new IllegalArgumentException("Lattitude '" + lat + "' is not numeric");
}
}

有无数的选项来实现 isNumeric 方法,some are discussed here 。这仍然会停止执行,但会向用户提供更清晰的信息。

B) 防御性设计

第二个选项是提供一个 parseLAttitude 方法,用 null 替换非数值:

public Double parseLattitude(String[] csvRow) {
final String lattitude = csvRow[4];
if(!isNumeric(lattitude)) {
System.out.println("Could not parse lattitude '" + lat + "'");
return null;
}
return Double.parseDouble(lat);
}

关于java - 从 CSV 解析 Double 会抛出 RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52437693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com