0

我正在尝试从 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);

机场数据.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 +
            '}';
}

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

4

2 回答 2

1

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

double在您的情况下,有人在您期望浮点数的 CSV 单元格中输入了一个非数字。Java 正确地抱怨这不能被解析为double. 您有两种方法来处理这种情况:

A) 合同设计

首先,您可以通过合同设计来解决这个问题。你可以有一个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实现方法有无数种选择,这里讨论了一些。这仍然会停止执行,但它会给用户一个更清晰的信息。

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);
}
于 2018-09-21T07:22:46.257 回答
0

我认为问题在于机场[4] 和机场[5] 可能不是机场的经纬度。如果这些字段包含字母字符 [az],您将收到 numberFormatException。您可以在打印机场时添加输出,例如:

System.out.println(Arrays.asList(airport));

问题?比我们能更好地帮助你

于 2018-09-21T07:14:00.390 回答