How can I prevent the role from being created again if it already exists? It must prevent character creation through importers and forms


red rum

The goal of this project is to create and organize data for TV shows.

In this project, users can directly import and enter data. The data is deployed in production and live.

If the role already exists (ie: the name is already in use), you must prevent the role from being created again. It must prevent character creation through importers and forms.

If the role is created with a form, it must display the error message in the same format as it already exists.

From what I understand, the code should:

  1. Use the input data and scan column 1 (character names) and use a while loop (maybe?) to see if it already exists.

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

  3. If the entered data does not exist then it will add it to the existing data.

So far I have written the following code to read a valid csv file:

Edit: Thanks to Sir Pleft, I got it to work using 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 problem is when I run the above code, I get the following error message:


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 row 1036, but my CSV file only has row 1035 at most

So I think the program is running? and get to the end of my csv and then don't know what to do.

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

left handed

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

Add pom.xmlthe following dependencies:

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

Then create Characterthe class that will contain a row of CSV. nameI've only added the and fields for convenience description, you can add other fields as well, or you can keep the namefield if you want.

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

Here's some code to do what you want: read through the csv file, if it finds a name that matches the given input, print an error, otherwise add 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


How can I prevent symlinks from being created inside it?

Jenny Hoy Running the following command works as expected and creates a symlink to srcfrom include/bb: ln -sf ../src include/bb However, calling the same command again will cause another symlink to be created srcin include/bbaka src/src. What can I do to prev

How can I prevent symlinks from being created inside it?

Jenny Hoy Running the following command works as expected and creates a symlink to srcfrom include/bb: ln -sf ../src include/bb However, calling the same command again will cause another symlink to be created srcin include/bbaka src/src. What can I do to prev

Can I prevent a folder with a specific name from being created?

Andrew I'm working on a LAMP web application and there is a scheduled process somewhere that keeps creating a folder shopin the root directory of the website . Every time this happens, it causes a conflict with the rewrite rules in the application, which is no

Can I prevent a reference to a type from being created or used?

Zhiyao My main goal is to prevent the creation of references of custom types. If it's not possible, then my secondary goal is to prevent the use of such references. I have a structure backed by virtual memory pages. Occasionally, the page may be remapped to a

Can I prevent a folder with a specific name from being created?

Andrew I'm working on a LAMP web application and there is a scheduled process somewhere that keeps creating a folder shopin the root directory of the website . Every time this happens, it causes a conflict with the rewrite rules in the application, which is no