0

The problem lies in the CVSParser class. I need to read the .CSV file then pass it off to the ArrayList<Club>

Example.csv
Team,W,D,L,GF,GA
Arsenal,24,7,7,68,41
Aston Villa,10,8,20,39,61
...

Club class

public Club(String team, int w, int l, int d, int GF, int GA) {
        ...
    }

CSVParser class

public static ArrayList<Club> CSVParser(String file) throws IOException{

        ArrayList<Club> Lists = new ArrayList<>();
        try{
        Scanner scanner = new Scanner(new FileReader(file));
        scanner.nextLine();

        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            //System.out.println(line);

            /*will get ArrayIndexOutOfBoundsException: 0 error */
            String[] splits = line.split(",");
            Lists.add(new Club(splits[0], Integer.parseInt(splits[1]),Integer.parseInt(splits[2]), 
                    Integer.parseInt(splits[3]), Integer.parseInt(splits[4]), Integer.parseInt(splits[5])));

        }scanner.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }

        System.out.println(Lists);
        return Lists;
2
  • 1
    Your post begins with "the problem is at the..." but your post neglects to even begin to describe a problem. Commented Mar 11, 2015 at 14:47
  • Why are you passing a FileReader to your Scanner instead of a File? Commented Mar 11, 2015 at 15:23

1 Answer 1

1

First, this is the right way to use scanner

Scanner s = new Scanner(input).useDelimiter(",");
int w = s.nextInt());

otherwise, just use a BufferedReader

Next, you have one or more empty lines in csvfile, you should check for the line length before splitting, something like:

if (line.trim().length() > 0) {
    String[] splits = line.split(",");
}
Sign up to request clarification or add additional context in comments.

Comments

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.