Solved adding if (line.isEmpty()) continue;
I need to read data from my text file and add this data into my ArrayList.
I see through debugger that the String[] words size is 1 which is "".
That is why I get an exception :
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
My code is
List<Bill> list = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("bill.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split(" ");
Integer id = Integer.parseInt(words[0]);
String venName = words[1];
Double amount = Double.parseDouble(words[2]);
LocalDate date = LocalDate.parse(words[3]);
BillType bt = BillType.valueOf(words[4]);
list.add(new Bill(venName, amount, date, bt, id));
}
} catch(IOException e) {
e.printStackTrace();
}
In this assignment I cannot use File and Object input/output Stream.
Could you please help me fix this bug?
line, does it contains space separated values?split, add aif (line.isEmpty()) continue;words.length == 5before you proceed with each iterationwordsbefore you attempt to extract values from it with magic numbers