O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. We will create a Plain Old Java Object (POJO) to represent the oil price in a given month and year:

 

        public class OilPrice{
          public String period;
          public Double value;
        }
  1. Next is to write a getOilData(String oilType) method, which will read the data from the given file and construct List<OilPrice>:

 

 private List<OilPrice> getOilData(String oilType) throws IOException{ Scanner reader = new Scanner(getClass() .getModule() .getResourceAsStream("com/packt/"+oilType) ); List<OilPrice> data = new LinkedList<>(); while(reader.hasNext()){ String line = reader.nextLine(); String[] elements = line.split("t"); OilPrice op = new OilPrice(); op.period = elements[0]; op.value = Double.parseDouble(elements[1]); data.add(op); } Collections.reverse(data); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required