How to solve this "Fixtures code" error exception?

• 12-02-2009, 03:05 AM
makerror
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.

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?
• 12-02-2009, 03:14 AM
sky
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.
• 12-02-2009, 03:25 AM
makerror
oh, after I click the error message, it shows at the red line(see at code again, I've edited it)
• 12-02-2009, 03:30 AM
Fubarable
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.
• 12-02-2009, 03:45 AM
sky
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.