Home > Software engineering >  How to prevent a character from being created again if it already exists? It must prevent creating c
How to prevent a character from being created again if it already exists? It must prevent creating c

Time:09-17

The project aim is to create and organize data for a tv show.

In this project, a user can import and enter data directly. The data is already deployed in production and active.

You have to prevent a character from being created again if it already exists (i.e: the name is already taken). It must prevent creating characters through the importer and the form.

If the character is created with the form, it must show an error message following the same format that is already in place.

From my understanding the code should:

  1. Use the data entered and scan it through column 1 (characters name) and with a while loop (maybe?) see if it already exists.

  2. If it does exist, the code should output: "Error this character exists"

  3. If it the data entered doesn't exists then it would add it onto the existing data.

So far I've coded the following to read the csv file which works:

Edit: Thanks to Sir Pleft I got it working with the following 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.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?

CodePudding user response:

If your project is maven then you can add OpenCSV library to help you

Add in your pom.xml the following dependency:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.3</version>
</dependency>

Then create the Character class which will hold one line of your CSV. For convenience I have added only the name and description fields, you can either add the others too or you can just leave the name field for your purposes.

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;
    }
}

And here is some code that performs what you are asking: Read through the csv file and if it finds a name that matches the given input it prints an error else it adds it to the csv file.

String inputName = "John"; //this is your input

List<Character> characters = new CsvToBeanBuilder(new FileReader("D:\\characters.csv"))
        .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("D:\\characters.csv");
    StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
    beanToCsv.write(characters);
    writer.close();
}
  • Related