title: Java - Reading from CSV File Error
body: <p>So my issue is I'm attempting to read and write from a CSV file but I'm having an issue with appending to a new line and then reading without getting the following error:</p>
  
  <p><a href="https://i.stack.imgur.com/1wW52.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1wW52.png" alt="enter image description here"></a></p>
  
  <p>Now I think I know why I'm getting the error but I can find a way to fix it. I'm writing to the file with the following code:</p>
  
  <pre><code>public void writeToFile(AbstractAccount account)
  {
      StringBuilder sb = new StringBuilder();
  
      sb.append(account.getFirstname() + ",");
      sb.append(account.getLastname() + ",");
      sb.append(account.getID() + ",");
      sb.append(account.getPhonenum() + ",");
      sb.append(account.getUsername() + ",");
      sb.append(account.getPassword() + ",");
      sb.append(account.getAccounttype());
  
      FileWriter fw = null;
      try
      {
          fw = new FileWriter("login.csv", true);
          fw.append(System.lineSeparator() + sb.toString());
          fw.close();
      } catch (IOException e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The issue is that the inside of the CSV file looks like this: </p>
  
  <pre><code>John,Doe,A1,0123456789,johnd,password1,Admin
  Jane,Doe,A2,1234567890,janed,password2,CourseCoordinator
  John,Smith,A3,2345678901,johns,password3,Approver
  Jane,Smith,A4,356789012,johns,password4,CasualStaff
  Test, Test, Test, Test, test, test, Admin
  </code></pre>
  
  <p>But when I copy it somewhere like into the stack overflow text editor I can see that the actual format is this:</p>
  
  <pre><code>John,Doe,A1,0123456789,johnd,password1,Admin
  Jane,Doe,A2,1234567890,janed,password2,CourseCoordinator
  John,Smith,A3,2345678901,johns,password3,Approver
  Jane,Smith,A4,356789012,johns,password4,CasualStaff
  
  Test, Test, Test, Test, test, test, Admin
  </code></pre>
  
  <p>So when I read from the file using this code:</p>
  
  <pre><code>public ArrayList&lt;AbstractAccount&gt; readFromFile(String filename)
  {
      BufferedReader br = null;
  
      String line = "";
  
      try
      {
          br = new BufferedReader(new FileReader(filename));
          while ((line = br.readLine()) != null)
          {
              // use comma as separator
              String[] attributes = line.split(",");
              accounts.add(CreateAccount(attributes));
          }
  
      } catch (FileNotFoundException e)
      {
          e.printStackTrace();
      } catch (IOException e)
      {
          e.printStackTrace();
      }
      return accounts;
  }
  </code></pre>
  
  <p>I get that error I mentioned before. I've tried a few ways of writing to a new line but I can't find one that doesn't give me that blank line. Anyone got any ideas?</p>
  
  <p>EDIT: Code for Create Account</p>
  
  <pre><code>public AbstractAccount CreateAccount(String[] data)
  {
      String firstname = data[0];
      String lastname = data[1];
      String id = data[2];
      String phonenum = data[3];
      String username = data[4];
      String password = data[5];
      String accounttype = data[6];
  
      if (accounttype.equals("Admin"))
          return new AdminModel(firstname, lastname, id, phonenum, username, password);
      else if (accounttype.equals("CourseCoordinator"))
          return new CourseCoordinatorModel(firstname, lastname, id, phonenum, username, password);
      else if (accounttype.equals("Approver"))
          return new ApproverModel(firstname, lastname, id, phonenum, username, password);
      else if (accounttype.equals("CasualStaff"))
          return new CasualStaffModel(firstname, lastname, id, phonenum, username, password);
      else
          return null;
  }
  </code></pre>