Skip to the content.

EPA Fuel Economy Data (Java Usage)

Contents

Source

http://www.fueleconomy.gov/feg/ws/index.shtml

Data services

Fuel prices

Code

import core.data.*;

public class FuelEconomy {
    public static void main(String[] args) {
        DataSource ds = DataSource.connectAs("xml", "http://www.fueleconomy.gov/ws/rest/fuelprices");      
        ds.load();
        ds.printUsageString();
        
        System.out.println( ds.fetchString("premium") );
    }
}

Output

The following data is available:
   a structure with fields:
   {
     cng : *
     diesel : *
     e85 : *
     electric : *
     lpg : *
     midgrade : *
     premium : *
     regular : *
   }
-----

3.11

All Vehicle Data

Code

// see bottom of page for Car class definition

DataSource ds = DataSource.connect("http://www.fueleconomy.gov/feg/epadata/vehicles.csv.zip");      
ds.load();
ds.printUsageString();

Car[] cs = ds.fetchArray("Car", "make", "model", "trany", "year", "city08", "highway08");
System.out.println( cs.length );
System.out.println( cs[0] );

Output

The following data is available:
   A list of:
     structures with fields:
     {
       UCity : *
       UCityA : *
       UHighway : *
       UHighwayA : *
       VClass : *
       atvType : *
       barrels08 : *
       barrelsA08 : *
       c240Dscr : *
       c240bDscr : *
       charge120 : *
       charge240 : *
       charge240b : *
       city08 : *
       city08U : *
       cityA08 : *
       cityA08U : *
       cityCD : *
       cityE : *
       cityUF : *
       co2 : *
       co2A : *
       co2TailpipeAGpm : *
       co2TailpipeGpm : *
       comb08 : *
       comb08U : *
       combA08 : *
       combA08U : *
       combE : *
       combinedCD : *
       combinedUF : *
       createdOn : *
       cylinders : *
       displ : *
       drive : *
       engId : *
       eng_dscr : *
       evMotor : *
       feScore : *
       fuelCost08 : *
       fuelCostA08 : *
       fuelType : *
       fuelType1 : *
       fuelType2 : *
       ghgScore : *
       ghgScoreA : *
       guzzler : *
       highway08 : *
       highway08U : *
       highwayA08 : *
       highwayA08U : *
       highwayCD : *
       highwayE : *
       highwayUF : *
       hlv : *
       hpv : *
       id : *
       lv2 : *
       lv4 : *
       make : *
       mfrCode : *
       model : *
       modifiedOn : *
       mpgData : *
       phevBlended : *
       phevCity : *
       phevComb : *
       phevHwy : *
       pv2 : *
       pv4 : *
       range : *
       rangeA : *
       rangeCity : *
       rangeCityA : *
       rangeHwy : *
       rangeHwyA : *
       sCharger : *
       startStop : *
       tCharger : *
       trans_dscr : *
       trany : *
       year : *
       youSaveSpend : *
     }
-----

39172
Car [make=Alfa Romeo, model=Spider Veloce 2000, transmission=Manual 5-spd, year=1985, cityMPG=19.0, hwyMPG=25.0]

Makes (year-specific)

Code

DataSource ds = DataSource.connectAs("xml", "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make");
ds.setParam("year", "2012");
ds.load();

ArrayList<String> makes = ds.fetchStringList("menuItem/value");
System.out.println(makes);

Model options

Code

    DataSource ds = DataSource.connectAs("xml", "http://www.fueleconomy.gov/ws/rest/vehicle/menu/options");
    ds.setParam("year", "2012").setParam("make", "Honda").setParam("model", "Fit");
    ds.load();

    // see bottom of page for MenuItem class definition
    MenuItem[] items =  ds.fetchArray("MenuItem", "menuItem/text", "menuItem/value");
    for (MenuItem m : items) {
        System.out.printf("%s\t\tID: %s\n", m.text, m.value);
    }

Output

Auto (S5), 4 cyl, 1.5 L     ID: 31819
Auto 5-spd, 4 cyl, 1.5 L		ID: 31818
Man 5-spd, 4 cyl, 1.5 L     ID: 31817

Specific Vehicle Data

See previous one for searching for vehicle IDs, or use https://www.fueleconomy.gov/feg/findacar.shtml to interactively search for a particular vehicle; then note the “id” query parameter in the URL that you are directed to.

Code

DataSource ds = DataSource.connectAs("xml", "http://www.fueleconomy.gov/ws/rest/vehicle/31819");
ds.load();

Car c = ds.fetch("Car", "make", "model", "trany", "year", "city08", "highway08");
System.out.println( c );

Output

Car [make=Honda, model=Fit, transmission=Automatic (S5), year=2012, cityMPG=27.0, hwyMPG=33.0]

Metadata (site-provided)


Additional class definitions

class Car {
    String make;
    String model;
    String transmission;
    int year;
    double cityMPG;
    double hwyMPG;
    
    public Car(String make, String model, String transmission, int year,
            double cityMPG, double hwyMPG) {
        super();
        this.make = make;
        this.model = model;
        this.transmission = transmission;
        this.year = year;
        this.cityMPG = cityMPG;
        this.hwyMPG = hwyMPG;
    }

    public String toString() {
        return "Car [make=" + make + ", model=" + model + ", transmission="
                + transmission + ", year=" + year + ", cityMPG=" + cityMPG
                + ", hwyMPG=" + hwyMPG + "]";
    }

}


class MenuItem {
    String text;
    String value;
    
    public MenuItem(String text, String value) {
        super();
        this.text = text;
        this.value = value;
    }
}