title: parsing csv file dealing with empty value java
body: <p>I am new to java and practicing parsing csv file. I've figured out how to parse and use a constructor to create an instance. However, there are empty strings in my csv file, and the console just throw <code>IllegalArgumentException: No enum constant</code> to me.</p>
  
  <p>I've referred to:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/6062925/enum-valueof-illegalargumentexception-no-enum-const-class">enum valueOf IllegalArgumentException: No enum const class</a></li>
  <li><a href="https://stackoverflow.com/questions/32068129/parse-csv-file-in-java-and-delaing-with-empty-values">Parse CSV file in java, and delaing with empty values</a></li>
  </ol>
  
  <p>Unfortunately, it doesn't work. When it comes to the <code>Person</code> with empty <code>Profession</code>, the program breaks. Do I need to fill some string like "NONE" for matching the enum, or there is any way to deal with this? Any help or hint is highly appreciated.</p>
  
  <p>Here is my <code>Profession</code> for example:</p>
  
  <pre><code>public enum Profession {
      DOCTOR, CEO, CRIMINAL, HOMELESS, UNEMPLOYED, MUSICIAN, BOXER , UNKNOWN, NONE;
  }
  </code></pre>
  
  <p>Here is how my csv file looks like:</p>
  
  <pre><code>  [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 ,           , FALSE   ,        ,      ,      , passenger
  animal, male  ,  4,         ,           , FALSE   ,        , dog  , TRUE , pedestrian
    .
    .
  </code></pre>
  
  <p>Here is the parsing code:</p>
  
  <pre><code>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(",",-1); 
      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"&amp;&amp; data.length == 1)) { 
          scenario = new Scenario();
          scenario.setLegalCrossing(clazz.endsWith("green"));
          continue;
      } else if ("person".equals(clazz)&amp;&amp; data.length == 10) {
          Profession professionEnum = Profession.valueOf(data[4].toUpperCase().trim());  
          Gender genderEnum = Gender.valueOf(data[1].toUpperCase().trim());
          BodyType bodyTypeEnum =BodyType.valueOf(data[3].toUpperCase().trim());
  
          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)) {
          Gender genderEnum = Gender.valueOf(data[1].toUpperCase().trim());
          BodyType bodyTypeEnum =BodyType.valueOf(data[3].toUpperCase().trim());
  
          animal = new Animal(Integer.parseInt(data[2]) , genderEnum , bodyTypeEnum, data[7]);
          animal.setIsPet(Boolean.parseBoolean(data[8]));
      }
  } catch (someException e) {
    e.printStackTrace();
  }
  </code></pre>
  
  <p>and the parsed line looks like:</p>
  
  <pre><code>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,,,,,,,,,
  </code></pre>