0

I am trying to implement a csv reader into my class in Java using eclipse. I keep getting an error for the add method "add(Person) in the type list is not applicable for the arguments (String[]). What am I doing wrong?

public static List<Person> readPersons(String fileName)
throws FileNotFoundException {
    
    
    int count = 0; 
    List<Person[]> content = new ArrayList<>(); 
    try(BufferedReader cv = new BufferedReader(new FileReader(fileName))){
        
        
        String line = "";
        while ((line = cv.readLine()) != null) {
            content.add(line.split(",")); 
        }
    } catch (FileNotFoundException e) {
        
        
    }
        
    return content;     
        
    }

Also, how do I implement this FileNotFoundException extender? It is required in the program.

1
  • 5
    line.split will return an array of String, not an array of Person. Commented Dec 7, 2020 at 7:56

2 Answers 2

1

The line.split( "," ) method will return an array of strings.

What it does is: The original string line is split into an array of strings. In that array, every string is a substring of line which is separated by a comma.

For example, if line is "Peter,Smith,38", the following array of strings will be returned: [ "Peter", "Smith", "38" ].

But, since your List only can contain objects of the type Person, it cannot take the String[] array returned by line.split( "," ).

So assuming you have an Constructor for Person that looks like this: Person( String firstName, String secondName, int age ) you would have to change your while loop to something like this:

while ( ( line = cv.readLine( ) ) != null )
{
  // Get the data from the CSV object
  String[] csvData = line.split( "," );
  
  // Create a Person object with the retrieved data
  Person personFromCsvLine = new Person( csvData[0], csvData[1], Integer.parseInt( csvData[2] );
  
  // Now you can add the person object to your list
  content.add( personFromCsvLine );
}
Sign up to request clarification or add additional context in comments.

8 Comments

I'm still getting the same error. It must have something to do with Eclipse??? Also, I think you miswrote the variable on the last line. Here is the error: "The method add(Person[]) in the type List<Person[]> is not applicable for the arguments (Person)"
@ReceptionistTWLawGroup This code is adding a single person in the add call, but you have declared the list to take an array of persons on each add call. Do you really want each entry in the list to be an array? This is nothing to do with Eclipse.
@greg-449 Actually no! It was my intention to add each line to a single array. It is a csv file with names seperated by age.
@ReceptionistTWLawGroup Then it should be List<Person> rather than List<Person[]> and the code in this answer looks correct for that.
Also, in the method signature you want to return a List<Person>. This won't be compatible to the type List<Person[]>, which is a list of arrays of persons and not a list of persons.
|
0

The answer by be-ta is correct.

I would like to point out that for reading a CSV you might want to consider using an existing solution in your code, like:

  1. Apache Commons CSV
  2. Jackson CsvMapper

These libraries will help with quoted / unquoted columns and conversion of values to the proper datatypes.

1 Comment

I'm still getting the same error. It must have something to do with Eclipse??? Also, I think he miswrote the variable on the last line. Here is the error: "The method add(Person[]) in the type List<Person[]> is not applicable for the arguments (Person)" –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.