I'm trying to get IntelliJ to display out the readings from a Data file that contains the date and gallons of water.
For some reason when I run it, it gives an error of "For input string: "0.7", which is the last data piece of the first line in my data file. I'm not sure how I'm reading it incorrectly.
Here's my initial reading file
public class WaterDataReading
{
private int gallons;
private String date;
public WaterDataReading(String line)
{
String fields [] = line.split(",");
date = fields[0].split(" ")[0];
gallons = Integer.valueOf(fields[1]);
}
public int getGallons()
{
return gallons;
}
public String getDate()
{
return date;
}
}
and then my readings file
public class WaterDataReadings extends ArrayList<WaterDataReading>
{
public WaterDataReadings(String filename) throws FileNotFoundException
{
Scanner fIn = new Scanner(new File(filename));
String line = "";
while (fIn.hasNextLine())
{
line = fIn.nextLine();
if (line.trim().length() == 0 || line.charAt(0) == '#')
{
continue;
}
this.add(new WaterDataReading(line));
}
}
}
And then finally where I output the readings (I'm working on a JavaFX project, but the readings should process and show up in the output console.
public class UiController implements Initializable
{
WaterDataReadings readings;
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
try
{
readings = new WaterDataReadings("./WaterData.csv");
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
System.exit(1);
}
for(WaterDataReading data: readings)
{
System.out.println(String.format("Date: %s \t Gallons: %s", data.getDate(), data.getGallons()));
}
}
}