Results 1 to 5 of 5
  1. #1
    makerror is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default How to solve this "Fixtures code" error exception?

    Hi,
    I'm new member starting today(and also new to Java)

    I plan to make a soccer league for next year. But before it runs, I must generate the fixtures list. So I try to search it a bit and found this code, but while I tried to run(using netbeans 6), it has an error :Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    I don't know what to do with this error because there's not an error guide line as to point out where is the error line.

    Java Code:
    /*
     * This code owes an enormous debt to
     * bluebones
     */
    
    import java.util.Arrays;
    
    public class Fixtures {
    
        public static void main(String[] args) {
    
            // Find out how many teams we want fixtures for.
            int teams = 0;
            try {
                [COLOR="Red"]teams = Integer.parseInt(args[0]);[/COLOR]
            } catch (NumberFormatException e) {
                System.err.println("Please state number of teams as first arg.");
                System.exit(0);
            }
    
            // If odd number of teams add a "ghost".
            boolean ghost = false;
            if (teams % 2 == 1) {
                teams++;
                ghost = true;
            }
    
            // Generate the fixtures using the cyclic algorithm.
            int totalRounds = teams - 1;
            int matchesPerRound = teams / 2;
            String[][] rounds = new String[totalRounds][matchesPerRound];
    
            for (int round = 0; round < totalRounds; round++) {
                for (int match = 0; match < matchesPerRound; match++) {
                    int home = (round + match) % (teams - 1);
                    int away = (teams - 1 - match + round) % (teams - 1);
                    // Last team stays in the same place while the others
                    // rotate around it.
                    if (match == 0) {
                        away = teams - 1;
                    }
                    // Add one so teams are number 1 to teams not 0 to teams - 1
                    // upon display.
                    rounds[round][match] = (home + 1) + " v " + (away + 1);
                }
            }
    
            // Interleave so that home and away games are fairly evenly dispersed.
            String[][] interleaved = new String[totalRounds][matchesPerRound];
    
            int evn = 0;
            int odd = (teams / 2);
            for (int i = 0; i < rounds.length; i++) {
                if (i % 2 == 0) {
                    interleaved[i] = rounds[evn++];
                } else {
                    interleaved[i] = rounds[odd++];
                }
            }
    
            rounds = interleaved;
    
            // Last team can't be away for every game so flip them
            // to home on odd rounds.
            for (int round = 0; round < rounds.length; round++) {
                if (round % 2 == 1) {
                    rounds[round][0] = flip(rounds[round][0]);
                }
            }
    
            // Display the fixtures
            for (int i = 0; i < rounds.length; i++) {
                System.out.println("Round " + (i + 1));
                System.out.println(Arrays.asList(rounds[i]));
                System.out.println();
            }
    
            System.out.println();
    
            if (ghost) {
                System.out.println("Matches against team " + teams + " are byes.");
            }
    
            System.out.println("Use mirror image of these rounds for "
                + "return fixtures.");
        }
    
        private static String flip(String match) {
            String[] components = match.split(" v ");
            return components[1] + " v " + components[0];
        }
    }
    could someone help me?
    Last edited by makerror; 12-02-2009 at 02:23 AM.

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    That exception is thrown when you try to access some positions of the array they are not defined. I'm not sure about the details how ArrayList and Vectors are implemented, but if it is a normal array (like C array) that means you have an array of 8 positions (for example) and you try to access to one possition different from 0,1,2,3,4,5,6,7 (arrays start in 0). In which line is the error happening? Summing up: at some point your code is accessing an ilegal position of an array in that line.

  3. #3
    makerror is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    oh, after I click the error message, it shows at the red line(see at code again, I've edited it)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that the args array passed to main holds no items -- has a length of 0. So when you try to access the first item in the array via args[0], the JVM complains that this simply doesn't exist. The problem is that the program requires that a command line parameter be passed to run it successfully, and in fact this parameter should be a number that represents the number of teams in the league. This command line parameter will be passed into the args array (the size of the array will depend on the number of parameters passed). I'm not sure how to run a program and pass parameters in NetBeans, but if you search the help file, it will surely tell you how to do this.

    Much luck.

  5. #5
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Indeed, if you want to improve the program, you should add some code that prevent this from happening. One possibility is to catch the exception ArrayIndexOutOfBounds, just in the same way as NumberFormatException, and another one is just check the length of the args array and make sure is not 0 before actually accessing to it.

Similar Threads

  1. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  2. Replies: 4
    Last Post: 06-07-2009, 12:59 AM
  3. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  4. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

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