2

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()));
        }
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Arsh Kaur
  • 31
  • 3

1 Answers1

1

You're getting a NumberFormatException because you are supplying a string representation of a floating point value to a method that is expecting a string representation of a Integer value. The dot (.) in the string is screwing things up.

Solution:

Since your data can obviously support floating point values use a Data Type that can support this as well like float or double. Change anything in code that is int gallons to double gallons.

To convert a string floating point value to a double data type (with validation) you would use:

String value = field[1];
if (value.matches("-?\\d+(\\.\\d+)?")) {
    gallons = Double.valueOf(value);
}
else {
    System.err.println("Invalid field value (" + value + ") provided for Gallons!");
    return; // or whatever...
}

OR

String value = field[1];
if (value.matches("-?\\d+(\\.\\d+)?")) {
    gallons = Double.parseDouble(value);
}
else {
    System.err.println("Invalid field value (" + value + ") provided for Gallons!");
    return; // or whatever...
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22