-2

I wrote a piece of code that appends the contents of all CSV files into a list. Compiling this program generates a "cannot find symbol" error at the line '''Iterator iter = full_files.iterator();'''

I checked my declarations and everything seems to be fine. I have declared and initialised full_files in the previous line.

public ArrayList<String[]> readCSV(File FDir) throws IOException {
        File[] files = FDir.listFiles();
        File F = files[0];
        ArrayList<String[]> all_list = new ArrayList<>();
        ArrayList<String[]> list = new ArrayList<>();

        // Creating a file to point to the "Full" directory
        File FullDir = new File(F.getParent() + "/Full/");
        File[] full_files = FullDir.listFiles();

        Iterator<File> iter = full_files.iterator();

        // Iterating through the files in "Full"
        while(iter.hasNext()) {
                File file = iter.next();
                if(GetFileExtension.get(file).equals(".csv")) {
                        list = CSVHandler.readCSV(file);
                        all_list.addAll(list);
                }
        }

        // Checking if the file outside "Full" is a CSV
        if(GetFileExtension.get(F).equals(".csv")) {
                list = CSVHandler.readCSV(F);
                all_list.addAll(list);
        }

        return all_list;

}

This is the error I get during compilation.

shared/utilities/main.java:82: error: cannot find symbol

    Iterator<File> iter = full_files.iterator();

                              ^
symbol:   method iterator()

location: variable fast of type File[]

1 error
  • 5
    The error says it all. You're trying to call iterator() on a variable of type File[] (i.e. an array of Files). But arrays don't have any iterator() method. All they have is the methods of Object and a length attribute. – JB Nizet Jun 16 '19 at 15:07

1 Answers1

0

First of all, you don't even need the Iterator, thanks to the for(File file : full_files) since Java 7.

This is a working version:

public List<String[]> readCSV(File dir) throws IOException {
  File[] files = dir.listFiles();
  List<String[]> allMatchingFiles = new ArrayList<>();

  if (files == null || files.length < 1) {
    return allMatchingFiles;
  }

  File firstFile = files[0];

  // Creating a file to point to the "Full" directory
  File fullDir = new File(firstFile.getParent() + "/Full/");
  File[] fullDirFiles = fullDir.listFiles();

  // Iterating through the files in "Full"
  if (fullDirFiles != null) {
    for (File file : fullDirFiles) {
      if (GetFileExtension.get(file).equals(".csv")) {
        allMatchingFiles.addAll(CSVHandler.readCSV(file));
      }
    }
  }

  // Checking if the file outside "Full" is a CSV
  if (GetFileExtension.get(firstFile).equals(".csv")) {
    allMatchingFiles.addAll(CSVHandler.readCSV(firstFile));
  }

  return allMatchingFiles;

}

As you might see, i wrote it a bit more defensively, checking for null, for example. Also I used List<> instead of ArrayList<>.

Furthermore it's common to use lowerCamelCase for variable names in Java.

@Joakim Danielson: Good Point, changed the code accordingly :)

Phe0nix
  • 157
  • 4
  • 15