-1

I'm currently writing a program that will read in a CSV file that contains data about age, height, and weight. My issue is, when I call in the method that reads the file from the main, it throws an IOException. I'm not sure what's causing this, and as this is for a project, I am not allowed to throw an IOException from the main() method. I inserted a output print line to try and spot where the bug occurs, because debug mode in eclipse is not allowing me to do so, and it seems that the error occurs before the readFile() method even starts.

I am extremely rusty in java and code in general, and am taking this class after not touching coding for an extended period of time. I'm not sure exactly where to start to try and debug this IOException.

public class Project1 {
    public static void main(String[] args) {

        //main method declaring project object
        Project1 project = new Project1();

        String fileName = project.checkArgs(args);
        File blah = null; //temporary name for the file don't pay attention to it

        try {
            blah = project.getFile(fileName); //gets the file and sets it to variable in main
        } 
        catch (FileNotFoundException e) { 
            e.printStackTrace();
        }

        try {
            project.readFile(blah, 500); //trying to read in file of 2d array in the readFileMethod
        }
        catch (IOException i) { //method gets caught here
            System.out.println("IOException");
        }
    }   

    //readFile METHOD
    public String[][] readFile(File file, int numRecords) throws IOException {

//two parameters: file, being read in this method, and numRecords the amount
//of elements in the array (I haven't done anything with numRecords yet and I
//believe this may possibly be the source of the error

        Scanner reader = new Scanner(file); //scanner to read in file

        //2d array to be passed back to main
        String[][] data = new String[numRecords][3]; 

        System.out.println("hi"); //test for IOException, doesn't get to this point 

        int iteration = 0; //to skip first line of text in csv file

        while (reader.hasNextLine()) { //loading in elements to 2d array
            iteration++;
            if (iteration < 1) {
                reader.nextLine();
                continue;
            }
            else {
                String list[] = reader.nextLine().trim().split(",");
                for (int i = 0; i < numRecords; i++) {
                    for (int j = 0; j < 3; j++) {
                        data[i][j] = list[j];
                        System.out.println(data[i][j]);
                    }
                }
            }
        }
        return data;
    }

The program catches the IOException in method main().

Abra
  • 19,142
  • 7
  • 29
  • 41
Jrobb
  • 1
  • 2
  • Edit your question and include the entire stack trace (as text, not as an image). – VGR Sep 01 '19 at 05:29

1 Answers1

0

I can't find the reason, please provide a stack trace (i.printStackTrace()).

Since you do not use the delimiters of the scanner, you may try another way:

BufferedReader br = new BufferedReader(new FileReader(file), bufferSize);
String line;

if ((line = br.readLine()) != null) {
   // process the line
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
olir
  • 66
  • 5