How can I prevent the role from being created again if it already exists? It must prevent character creation through importers and forms
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:
Use the input data and scan column 1 (character names) and use a while loop (maybe?) to see if it already exists.
If it does exist, the code should output: "Error this character exists"
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?
If your project is Maven then you can add OpenCSV library to help you
Add pom.xml
the following dependencies:
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.3</version>
</dependency>
Then create Character
the class that will contain a row of CSV. name
I've only added the and fields for convenience description
, you can add other fields as well, or you can keep the name
field 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();
}