Parsing Double from CSV throws RuntimeException

224 views Asked by At

I am trying to read the data from CSV file. everything works fine but when i try to read the data for longitude and latitude. it gives me an below error message.

 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 +
            '}';
}

**Any solution for this problem would be greatly appreciated....

2

There are 2 answers

1
Stephan Hogenboom On

I think the problem is that airport[4] & airport[5] might not be the lon and lat of the airport. If these field contain alpha characters [a-z] you will get an numberFormatException. Can you add the the output when you print airport like:

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

to the question? Than we will be better able to help you

0
Maximilian C. On

The problem with CSV files is, that users (and other programmers) can pretty much write into the file what they want.

In your case, somebody entered a non-number into a CSV cell where you were expecting a double floating point number. Java correctly complains that this cannot be parsed to double. You have two approaches, to treat such situations:

A) Design by Contract

First, you could resolve that by relying on design by contract. You could have a parseLat method:

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");
    }
}

There are countless options to implement an isNumeric method, some are discussed here. This will still stop the execution, but it will give a clearer message to users.

B) Defensive Design

A second option, is to provide a parseLAttitude method, that replaces non numeric value with 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);
}