Exception in thread "main" java.lang.RuntimeException: Error parsing my last line of CSV

I'm working on a project where I am trying to use data imported by user and check if it already exists in column 1. If not then it prints an error message.

  1. Use the data entered and scan it through column 1 (Name) and see if it already exists.
  2. If it does exist, the code should output: "Error this character exists"
  3. If the data entered doesn't exists then it should add it onto the existing data.

So far I've got the class to hold the CSV:

package com.openCSV.mavenproject;

import com.opencsv.bean.CsvBindByPosition;

public class Character {

    @CsvBindByPosition(position = 0)
    private String name;

    @CsvBindByPosition(position = 1)
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

    

Below is the actual code.

package com.openCSV.mavenproject;

import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IllegalStateException, IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
        // TODO Auto-generated method stub
        
        String inputName = "John"; //this is your input
        
        String file = "/Users/apple/eclipse-workspace/Test/data/characters.csv";
        
    //Convert CSV to bean

        List<Character> characters = new CsvToBeanBuilder(new FileReader(file))
                .withType(Character.class)
                .withSkipLines(1)
                .build()
                .parse();

        characters.forEach(System.out::println);

        boolean anyMatch = characters.stream().anyMatch(character -> character.getName().equals(inputName));

        if (anyMatch) {
            System.out.println("Error this character exists");
        } else {
            Character character = new Character();
            character.setName(inputName);
            characters.add(character);
            Writer writer = new FileWriter(file);
            StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
            beanToCsv.write(characters);
            writer.close();
        }

    }

}

Now my issue is when I run the above code, I get the error message below:

Exception in thread "main" java.lang.RuntimeException: Error parsing CSV line: 1036, values: 

    at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:376)
    at com.openCSV.mavenproject.Main.main(Main.java:31)
Caused by: com.opencsv.exceptions.CsvMalformedLineException: Unterminated quoted field at end of CSV line. Beginning of lost text: [
]
    at com.opencsv.CSVReader.readNext(CSVReader.java:355)
    at com.opencsv.bean.CsvToBean.submitAllBeans(CsvToBean.java:296)
    at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:357)
    ... 1 more

It specifies line 1036, however my CSV file is only up to line 1035

So I think the program is working? and reaching the end of my csv and then doesn't know what to do.

How would I edit my code so that it breaks after the csv file finishes?

Any help is appreciated!

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum