Reputation: 1386
I am new to Java and practice parsing csv file. I do understand what does IndexOutOfBound
means, but I don't understand why my parsed data cannot do like all the tutorials I've visited such as https://examples.javacodegeeks.com/java-csv-parsing-example/
I can only read the first column, which is data[0]
. There must be something wrong with my parsing method, but I cannot figure it out. Any help or hint is hight appreciated.
my data file looks like this:
[0], [1], [2], [3] , [4] , [5] , [6] , [7] , [8] , [9]
class, gender, age, bodyType, profession, pregnant, isYou ,species, isPet, role
scenario:green, , , , , , , ,
person, female, 24, average , doctor , FALSE , , , , passenger
animal, male , 4, , , FALSE , , dog , TRUE , pedestrian
.
.
I tried to parse like this:
ArrayList<String> csvContents = new ArrayList<String>();
try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) {
String headerLine = csvReader.readLine(); //get rid of header
while ((line = csvReader.readLine()) != null) {
csvContents.add(line);// add the line to the ArrayList
}
for (String csvLine : csvContents) {
// split by comma and remove redundant spaces
String[] data = csvLine.split("\\s*,\\s*");
System.out.println(data[1]);// IndexOutOfBound
Character character = null;
String clazz = data[0].toLowerCase();// cannot use word "class" as a variable
Profession professionEnum = Profession.valueOf(data[4].toUpperCase());
Gender genderEnum = Gender.valueOf(data[1].toUpperCase());
BodyType bodyTypeEnum =BodyType.valueOf(data[3].toUpperCase());
if (clazz.startsWith("scenario")) {
scenario = new Scenario();
scenario.setLegalCrossing(clazz.endsWith("green"));
continue;
} else if ("person".equals(clazz)) {
person = new Person(Integer.parseInt(data[2]), professionEnum ,genderEnum , bodyTypeEnum , Boolean.parseBoolean(data[5]));
person.setAsYou(Boolean.parseBoolean(data[6]));
} else if ("animal".equals(clazz)) {
animal = new Animal(Integer.parseInt(data[2]) , genderEnum , bodyTypeEnum, data[7]);
animal.setIsPet(Boolean.parseBoolean(data[8]));
}
} catch (someException e) {
e.printStackTrace();
}
EDIT
print out csvLine
before split:
scenario:green,,,,,,,,,
person,female,24,average,doctor,false,false,,,passenger
person,male,40,overweight,unknown,false,false,,,passenger
person,female,2,average,,false,false,,,passenger
person,male,82,average,,false,false,,,pedestrian
person,female,32,average,ceo,true,false,,,pedestrian
person,male,7,athletic,,false,false,,,pedestrian
animal,male,4,,,false,false,dog,true,pedestrian
scenario:red,,,,,,,,,
Upvotes: 0
Views: 212
Reputation: 1386
I've figured it out. It's counterintuitive for me, though. I need to specify the length
of the data
array parsed to put every attribute like this:
ArrayList<String> csvContents = new ArrayList<String>();
try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) {
String headerLine = csvReader.readLine(); //get rid of header
while ((line = csvReader.readLine()) != null) {
csvContents.add(line);// add the line to the ArrayList
}
for (String csvLine : csvContents) {
// split by comma and remove redundant spaces
String[] data = csvLine.split("\\s*,\\s*");
System.out.println(data[1]);// IndexOutOfBound
Character character = null;
String clazz = data[0].toLowerCase();// cannot use word "class" as a variable
if (clazz.startsWith("scenario"&& data.length == 1)) {
scenario = new Scenario();
scenario.setLegalCrossing(clazz.endsWith("green"));
continue;
} else if ("person".equals(clazz)&& data.length == 10) {
Profession professionEnum = Profession.valueOf(data[4].toUpperCase());
Gender genderEnum = Gender.valueOf(data[1].toUpperCase());
BodyType bodyTypeEnum =BodyType.valueOf(data[3].toUpperCase());
person = new Person(Integer.parseInt(data[2]), professionEnum ,genderEnum , bodyTypeEnum , Boolean.parseBoolean(data[5]));
person.setAsYou(Boolean.parseBoolean(data[6]));
} else if ("animal".equals(clazz)) {
animal = new Animal(Integer.parseInt(data[2]) , genderEnum , bodyTypeEnum, data[7]);
animal.setIsPet(Boolean.parseBoolean(data[8]));
}
} catch (someException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 167
you need to fill comlete data for all cells in a row.
For eg. first line in your csv, contains only 1 cell having value scenario:green which is data[0].
If you fill in data for all other cells in your csv, your will start receiving data[1], data[2], data[3]....
Upvotes: 1
Reputation: 1245
After spliting, the data just have one element, so that when you access data[1], then you get exception. Solution: try with another regex like "," only.
Ps: your csv is malformed at
scenario:green, , , , , , , , Try to put one more ","
Upvotes: 1