title: Problem with Array Index Out Of Bounds while reading a file with BufferedReader
body: <p>I have to write a program which calculates the Twelve Days of Christmas. The program requests an integer from 1 through 12 and then lists the gifts for that day along with the cost. The program should also CALCULATE the total cost of all twelve days. The program should read information from a text file. </p>
  
  <p>My code: </p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Scanner;
  import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.Scanner;
  
  public class TwelveDaysOfChristmas {
  
      private String[] nameDays = new String[12];
      private int[] costDays = new int[12];
  
      public void readData(String fileName)
  
      {
          String[] name = new String[12];
          int[] total = new int[12];
  
          BufferedReader bufferedReader = null;
          String line = null;
          int countNum = 0;
  
          try
          {
              bufferedReader = new BufferedReader(new FileReader(fileName));
  
              while((line = bufferedReader.readLine()) != null)
              {
                  String[] data = line.split(",");
                  name[countNum] = data[0];
                  total[countNum] = Integer.parseInt(data[1]);
                  countNum++;
              }
  
              if(fileName == "nameDays.txt")
              {
                  nameDays = name;
                  costDays = total;
              }
              else
              {
                  System.out.println("Error ocurred.Not days encountered.");
              }
  
              if(fileName == "costDays.txt")
              {
                  costDays = total;
              }
              else
              {
                  System.out.println("Not cost encountered.");
              }       
  
              bufferedReader.close();
          }
          catch(FileNotFoundException e)
          {
              System.out.println("File Not Found");
          }
          catch(IOException e)
          {
              System.out.println("Error input file.");
          }
      }
  
      public void setNameData()
  
      {
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Enter day: ");
          int day = keyboard.nextInt();
  
          if(Arrays.asList(nameDays).contains(day))
          {
              int index = Arrays.asList(nameDays).indexOf(day);
              int ocurrences = costDays[index];
  
              System.out.println("User Input: " + day);
              System.out.println("Sample output: ");
              System.out.println("Your gifts for Day " + day + " are:");
              System.out.println((index + day) + "\n" + ocurrences);
          }
          else
          {
              System.out.println("Error try again :D");
          }
  
          if(Arrays.asList(costDays).contains(day))
          {
              int index = Arrays.asList(costDays).indexOf(day);
              int ocurrences = costDays[index];
  
              System.out.println("cost of day: " + day);
              System.out.println("Total cost of twelve days: ");
              System.out.println("Your gifts for Day " + day + " are:");
              System.out.println((index + day) + "\n" + ocurrences);
          }
          else
          {
              System.out.println("Error try again :D");
          }
  
      }
  }
  </code></pre>
  
  <p>My drive program</p>
  
  <pre><code>public class Test {
  
      public static void main(String[]args)
      {
          TwelveDaysOfChristmas user = new TwelveDaysOfChristmas();
          user.readData("nameDays.txt");
          user.readData("costDays.txt");
          user.setNameData();
      }
  }
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
      at TwelveDaysOfChristmas.readData(TwelveDaysOfChristmas.java:32)
      at Test.main(Test.java:7)
  </code></pre>
  
  <p>Text Files:</p>
  
  <pre><code>nameDays.txt
  One Patridge in a Pear Tree,
  Two Turtle Doves,
  Three French Hen,
  Four Calling Birds,
  Five Gold Rings,
  Siz Geese-a-Laying,
  Seven Swans-a-Swimming,
  Eight Maids-a-Milking,
  Nine Ladies Dancing,
  Ten Lords-a-Leaping,
  Eleven Piper,
  Twelve Drummers Drumming
  
  costDays.txt
  220.13,
  375.00,
  181.50,
  599.96,
  750.00,
  390.00,
  13125.00,
  58.00,
  7552.84,
  10000.00,
  2804.40,
  3038.10
  </code></pre>