Results 1 to 2 of 2
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException Error

    I'm getting an ArrayIndexOutOfBoundsException Error in this line of code. It's actually happening with this line of code.

    Java Code:
      for (int n = 1; n <= numberOfteams; n++)
            {
            System.out.println("Enter Team Name " + (n) + ": ");
            teamNames[n - 1] = keyboard.next();
           {
    How would I go about fixing this? Please and thank you.

    Java Code:
    import java.util.Scanner;
    
    public class fantasyFootball
    {
    
        private int numberOfteams; 
        private int numberOfweeks; 
    
        private int[][] table;
        private double[] teamAverage;
        private double[] weeklyAverage;
        private String[] teamNames = new String[numberOfteams];
    
        public fantasyFootball(int[][] a)
        {
            if (a.length == 0 || a[0].length == 0)
            {
                System.out.println("Empty table records. Aborting.");
                System.exit(0);
            }
    
            numberOfteams = a.length;
            numberOfweeks = a[0].length;
            filltable(a);
            fillteamAverage( );
            fillweeklyAverage( );
        }
    
        public fantasyFootball(fantasyFootball score2)
        {
            numberOfteams = score2.numberOfteams;
            numberOfweeks = score2.numberOfweeks;
            filltable(score2.table);
            fillteamAverage( );
            fillweeklyAverage( );
        }
     
        public fantasyFootball( )
        {
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Please enter the number of teams:");
            numberOfteams = keyboard.nextInt();
            keyboard.nextLine();
    
            System.out.println("Please enter the number of weeks:");
            numberOfweeks = keyboard.nextInt();
            keyboard.nextLine();
    
            table = new int[numberOfteams][numberOfweeks];
            
            for (int n = 1; n <= numberOfteams; n++)
            {
            System.out.println("Enter Team Name " + (n) + ": ");
            teamNames[n] = keyboard.next();
    
            for (int teamName = 1; 
                         teamName <= numberOfteams; teamName++)
                for (int weeklyNumber = 1; 
                                weeklyNumber <= numberOfweeks; weeklyNumber++)
                {
                    System.out.println("Enter score for student number "
                                                 + teamName);
                    System.out.println("on quiz number " + weeklyNumber);
                    table[teamName - 1][weeklyNumber - 1] =
                                                     keyboard.nextInt( );
                }
            }
    
            fillteamAverage( );
            fillweeklyAverage( );
        }
    
        private void filltable(int[][] a)
        {
            table = new int[numberOfteams][numberOfweeks];
    
            for (int teamName = 1; 
                            teamName <= numberOfteams; teamName++)
            {
                for (int weeklyNumber = 1; 
                             weeklyNumber <= numberOfweeks; weeklyNumber++)
                    table[teamName][weeklyNumber] = 
                                            a[teamName][weeklyNumber];
            }
        }
    
        private void fillteamAverage( )
        {
            teamAverage = new double[numberOfteams];
    
            for (int teamName = 1; 
                            teamName <= numberOfteams; teamName++)
            {
                double sum = 0;
                for (int weeklyNumber = 1; 
                             weeklyNumber <= numberOfweeks; weeklyNumber++)
                    sum = sum + table[teamName - 1][weeklyNumber - 1];
              
                teamAverage[teamName - 1] = sum/numberOfweeks;
              
            }
        }
    
        private void fillweeklyAverage( )
        {
            weeklyAverage = new double[numberOfweeks];
    
            for (int weeklyNumber = 1; weeklyNumber <= numberOfweeks; weeklyNumber++)
            {
                double sum = 0;
                for (int teamName = 1; 
                              teamName <= numberOfteams; teamName++)
                    sum = sum + table[teamName - 1][weeklyNumber - 1];
           
                weeklyAverage[weeklyNumber - 1] = sum/numberOfteams;
            
            }
        }
    
        public void display( )
        {
          	System.out.print("Team name");
        	for (int i = 1; i <= numberOfweeks; i++){
        	System.out.print("Week " + i);
        	}
        	
            for (int t = 1;  t <= numberOfteams; t++)
            {
                System.out.print(teamNames[t]);
                for (int weeklyNumber = 1; 
                                 weeklyNumber <= numberOfweeks; weeklyNumber++)
                  System.out.print(table[t - 1][weeklyNumber - 1] + " ");
                System.out.println(" Ave = " + teamAverage[t - 1] );
            }
    
            System.out.println("Quiz averages: ");
            for (int weeklyNumber = 1; weeklyNumber <= numberOfweeks; weeklyNumber++)
                System.out.print("Quiz " + weeklyNumber 
                                + " Ave = " + weeklyAverage[weeklyNumber - 1] + " ");
            System.out.println( );
        }
    
         public static void main(String[] args) 
         {
        	 fantasyFootball fantasy = new fantasyFootball( );
             fantasy.display( );
         }
     }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: ArrayIndexOutOfBoundsException Error

    Your first problem is that instance variables of type int are defaulted to zero. So numberOfteams has the default value of zero. Then you use that to allocate the teamNames array. So the array size is of size 0. So allocate the array after you get the number of teams from the user.

    The next problem to fix is line 130 where you still use t and not t-1 to reference the teamNames array.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException ERROR?
    By dharav in forum New To Java
    Replies: 5
    Last Post: 01-15-2012, 10:33 AM
  2. ArrayIndexOutOfBoundsException error
    By Dreaming in forum New To Java
    Replies: 2
    Last Post: 10-31-2011, 12:09 AM
  3. Replies: 1
    Last Post: 09-27-2011, 07:47 PM
  4. Replies: 11
    Last Post: 02-22-2011, 06:23 PM
  5. Replies: 1
    Last Post: 08-01-2007, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •