title: Problems reading a CSV file in Java. Only the first line is read
body: <p>For a Java homework assignment, I need to create a class that reads and writes CSV files. I'm currently having some problems reading the the CSV. The code below, only outputs the first line of the code and then generates the following error message: <em>'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.gc01.FileManager.CSVManager.main(CSVManager.java:27)".</em></p>
  
  <p>I have looked at various examples, and I am aware of the 'opencsv' package, but I need to write this code myself. I have located the problem to the statement "System.out.print(data[i]);". However, when cross-referencing this code it all seems to be fine.</p>
  
  <p>I am using the methods from the FileInput class, as specified by my teacher (<a href="http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java" rel="nofollow">http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java</a>).</p>
  
  <pre><code>public class CSVManager {
      public static void main(String [] args){
  
          Scanner sc = new Scanner (System.in);
          System.out.println("Please enter the file directory of the chosen CSV");
          System.out.println("For Example: /Users/UserName/Downloads/FileName.csv");
          ///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
          final String fileName = sc.nextLine();
          System.out.println("How many columns?");
          final int columns = sc.nextInt();
  
          BufferedReader br = null;
          String line = "";
          String splitBy = " , ";
  
          try {
              br = new BufferedReader(new FileReader(fileName));
              while((line = br.readLine()) != null) {
                  for (int i = 0; i &lt; columns; i++) {
                      String[] data = line.split(splitBy);
                      System.out.print(data[i]);
                  }
              }
          } catch (FileNotFoundException e){
              e.printStackTrace();
          } catch (IOException e) {   
              e.printStackTrace();  
          } finally {  
              if (br != null) {  
              try {  
                      br.close();  
                  } catch (IOException e) {  
                      e.printStackTrace();  
                  }
              }
          }
          System.out.println("File Read");
      }
  }
  </code></pre>