![]() |
|
|
|||||||
![]() |
Java - generating soccer fixtures from a list of teams |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
public void createFixtureList()
{ //setup 'cal' to get todays date Calendar cal = new GregorianCalendar(); int y = cal.get(Calendar.YEAR); int m = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ... m++; // 1=Jan, 2=Feb, .... int d = cal.get(Calendar.DAY_OF_MONTH); int hr = 15; int min = 00; int j = league.size(); j--; // last club in league will have already had fixtures generated by previous clubs for (int i=0; i<j; i++) { //get a CLUB(i) and create all possible fixtures for it Club c1 = (Club)league.get(i); for (int x=i+1; x<league.size(); x++) { // get an opponent club - CLUB(x) to create 2 fixtures (home/away) with club(i) Club c2 = (Club)league.get(x); // HOME GAME - CLUB(i) vs CLUB(x) // set game date for one week after the previous club(i) matchdate d = d+7; if (d > 23) // correct values 31->1 { m++; d=(d-30); if (m > 11) // correct values dec->jan { m = 1; y++; } } Calendar homedate = new GregorianCalendar(y, m, d, hr, min); Fixture f1 = new Fixture(c1.getGround(), homedate, c1, c2); fixtures.add(f1); // AWAY GAME CLUB(x) vs CLUB(i) // set away game for 6 months later int tempM = m; int tempY = y; m = (m+6); if (m > 12) { m=(m-12); y=y+1; } Calendar awaydate = new GregorianCalendar(y, m, d, hr, min); Fixture f2 = new Fixture(c2.getGround(), awaydate, c2, c1); fixtures.add(f2); // return m and y back to previous CLUB(i) homegame value m = tempM; y = tempY; homedate.clear(); homedate.set(y,m,d,hr,min); } } printFixtureList(); } For some reason, the above code worked perfectly when I had 3 clubs in the league. But since I added a 4th club, I always get a runtime error. It generates all fixtures ( the problem occurs when it tries to increment i and get the next club to be club(i). When it tries to get the club, it throws a 'null pointer exception'. So the problem seems to be with the first FOR statement in the above code. zcraven |
|
|
|
|
#2 |
|
Posts: n/a
|
"zcraven" <> wrote in message news:cltm1e$hr$... > public void createFixtureList() > { > //setup 'cal' to get todays date > Calendar cal = new GregorianCalendar(); > int y = cal.get(Calendar.YEAR); > int m = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ... > m++; // 1=Jan, 2=Feb, > ... > int d = cal.get(Calendar.DAY_OF_MONTH); > int hr = 15; > int min = 00; > > int j = league.size(); > j--; // last club in league will have already had fixtures > generated by previous clubs > for (int i=0; i<j; i++) > { > //get a CLUB(i) and create all possible fixtures for it > Club c1 = (Club)league.get(i); > for (int x=i+1; x<league.size(); x++) > { > // get an opponent club - CLUB(x) to create 2 fixtures > (home/away) with club(i) > Club c2 = (Club)league.get(x); > > // HOME GAME - CLUB(i) vs CLUB(x) > // set game date for one week after the previous club(i) > matchdate > d = d+7; > if (d > 23) // correct values 31->1 > { > m++; > d=(d-30); > if (m > 11) // correct values dec->jan > { > m = 1; > y++; > } > } > Calendar homedate = new GregorianCalendar(y, m, d, hr, min); > Fixture f1 = new Fixture(c1.getGround(), homedate, c1, c2); > fixtures.add(f1); > > // AWAY GAME CLUB(x) vs CLUB(i) > // set away game for 6 months later > int tempM = m; > int tempY = y; > m = (m+6); > if (m > 12) > { > m=(m-12); > y=y+1; > } > Calendar awaydate = new GregorianCalendar(y, m, d, hr, min); > > Fixture f2 = new Fixture(c2.getGround(), awaydate, c2, c1); > fixtures.add(f2); > > // return m and y back to previous CLUB(i) homegame value > m = tempM; > y = tempY; > homedate.clear(); > homedate.set(y,m,d,hr,min); > } > } > printFixtureList(); > } > > > For some reason, the above code worked perfectly when I had 3 clubs in the > league. But since I added a 4th club, I always get a runtime error. > > It generates all fixtures ( > the problem occurs when it tries to increment i and get the next club to be > club(i). When it tries to get the club, it throws a 'null pointer > exception'. So the problem seems to be with the first FOR statement in the > above code. > correction - the problem is not accessing the clubs - it does that fine. the problem is with updating the date values. when throws an 'error: month cannot be zero or negative' error when it tries to create a fixture for the 2nd club vs the 3rd club in the league. |
|
|
|
#3 |
|
Posts: n/a
|
ok forget all that code before, it was crap. The problem is that it is not
updating the dates as I want it to. The first error (see printout below) occurs here: Man Utd vs Arsenal @ Old Trafford 24-May-2004 @ 15:00 (03-May-2004 @ 15:00 is correct) Basically I want to create a whole fixture list, and I dont want the same club to have two games on the same date. Whats wrong with my maths? This is the code: public void createFixtureList() { // set up todays date for later reference Calendar today = new GregorianCalendar(); today.set(Calendar.MONTH, 2); //temp today.set(Calendar.HOUR_OF_DAY, 15); today.set(Calendar.MINUTE, 00); today.set(Calendar.SECOND, 00); //set up 'matchdate' as a date object with todays date, and 15:00 Calendar matchdate = new GregorianCalendar(); matchdate.set(Calendar.MONTH, 2); //temp matchdate.set(Calendar.HOUR_OF_DAY, 15); matchdate.set(Calendar.MINUTE, 00); matchdate.set(Calendar.SECOND, 00); int j = league.size(); j--; // no need to create fixtures for last club in league, as all will have been generated already by previous clubs for (int i=0; i<j; i++) { //get a CLUB(i) and create all possible fixtures for it Club c1 = (Club)league.get(i); System.out.println(" i=" + i); for (int x=i+1; x<league.size(); x++) { // get an opponent club - CLUB(x) to create 2 fixtures (home/away) with club(i) Club c2 = (Club)league.get(x); // HOME GAME - CLUB(i) vs CLUB(x) // set game date for one week after the previous club(i) matchdate (or today if this is 1st game) matchdate.add(Calendar.DATE, 7); Fixture f1 = new Fixture(c1.getGround(), matchdate, c1, c2); fixtures.add(f1); System.out.println(f1.getFixtureClubs() + " @ " + f1.getFixturePlace()); f1.displayFixtureDate(); // AWAY GAME CLUB(x) vs CLUB(i) // set away game for exactly 6 months after the home game // awaydate = homedate; matchdate.add(Calendar.MONTH, 6); Fixture f2 = new Fixture(c2.getGround(), matchdate, c2, c1); fixtures.add(f2); System.out.println(f2.getFixtureClubs() + " @ " + f2.getFixturePlace()); f2.displayFixtureDate(); matchdate.add(Calendar.MONTH, -6); } // before getting next club(i), give two weeks space to avoid having 2 matches on the same date matchdate = today; matchdate.add(Calendar.DATE, 14); System.out.println("reset date to today, then added 14"); } printFixtureList(); } this is the system.out: [ league1.createFixtureList() ] i=0 Liverpl vs Chelsea @ Anfield 05-Apr-2004 @ 15:00 Chelsea vs Liverpl @ chelseas ground 05-Oct-2004 @ 15:00 Liverpl vs Man Utd @ Anfield 12-Apr-2004 @ 15:00 Man Utd vs Liverpl @ Old Trafford 12-Oct-2004 @ 15:00 Liverpl vs Arsenal @ Anfield 19-Apr-2004 @ 15:00 Arsenal vs Liverpl @ Highbury 19-Oct-2004 @ 15:00 Liverpl vs Newcastle @ Anfield 26-Apr-2004 @ 15:00 Newcastle vs Liverpl @ St.James Park 26-Oct-2004 @ 15:00 reset date to today, then added 14 i=1 Chelsea vs Man Utd @ chelseas ground 19-Apr-2004 @ 15:00 Man Utd vs Chelsea @ Old Trafford 19-Oct-2004 @ 15:00 Chelsea vs Arsenal @ chelseas ground 26-Apr-2004 @ 15:00 Arsenal vs Chelsea @ Highbury 26-Oct-2004 @ 15:00 Chelsea vs Newcastle @ chelseas ground 03-May-2004 @ 15:00 Newcastle vs Chelsea @ St.James Park 03-Nov-2004 @ 15:00 reset date to today, then added 14 i=2 Man Utd vs Arsenal @ Old Trafford 24-May-2004 @ 15:00 Arsenal vs Man Utd @ Highbury 24-Nov-2004 @ 15:00 Man Utd vs Newcastle @ Old Trafford 31-May-2004 @ 15:00 Newcastle vs Man Utd @ St.James Park 30-Nov-2004 @ 15:00 reset date to today, then added 14 i=3 Arsenal vs Newcastle @ Highbury 20-Jun-2004 @ 15:00 Newcastle vs Arsenal @ St.James Park 20-Dec-2004 @ 15:00 reset date to today, then added 14 |
|
|
|
#4 |
|
Posts: n/a
|
zcraven wrote:
> ok forget all that code before, it was crap. The problem is that it is not > updating the dates as I want it to. The first error (see printout below) > occurs here: > > Man Utd vs Arsenal @ Old Trafford > 24-May-2004 @ 15:00 > > (03-May-2004 @ 15:00 is correct) > > Basically I want to create a whole fixture list, and I dont want the same > club to have two games on the same date. Whats wrong with my maths? > > This is the code: > > > public void createFixtureList() > { > // set up todays date for later reference > Calendar today = new GregorianCalendar(); > today.set(Calendar.MONTH, 2); //temp > today.set(Calendar.HOUR_OF_DAY, 15); > today.set(Calendar.MINUTE, 00); > today.set(Calendar.SECOND, 00); > > //set up 'matchdate' as a date object with todays date, and 15:00 > Calendar matchdate = new GregorianCalendar(); > matchdate.set(Calendar.MONTH, 2); //temp > matchdate.set(Calendar.HOUR_OF_DAY, 15); > matchdate.set(Calendar.MINUTE, 00); > matchdate.set(Calendar.SECOND, 00); > > int j = league.size(); > j--; // no need to create fixtures for last club in league, as all > will have been generated already by previous clubs > for (int i=0; i<j; i++) > { > //get a CLUB(i) and create all possible fixtures for it > Club c1 = (Club)league.get(i); > System.out.println(" i=" + i); > for (int x=i+1; x<league.size(); x++) > { > > // get an opponent club - CLUB(x) to create 2 fixtures > (home/away) with club(i) > Club c2 = (Club)league.get(x); > > // HOME GAME - CLUB(i) vs CLUB(x) > // set game date for one week after the previous club(i) > matchdate (or today if this is 1st game) > matchdate.add(Calendar.DATE, 7); > Fixture f1 = new Fixture(c1.getGround(), matchdate, c1, c2); > fixtures.add(f1); > System.out.println(f1.getFixtureClubs() + " @ " + > f1.getFixturePlace()); > f1.displayFixtureDate(); > > // AWAY GAME CLUB(x) vs CLUB(i) > // set away game for exactly 6 months after the home game > // awaydate = homedate; > matchdate.add(Calendar.MONTH, 6); > Fixture f2 = new Fixture(c2.getGround(), matchdate, c2, c1); > fixtures.add(f2); > System.out.println(f2.getFixtureClubs() + " @ " + > f2.getFixturePlace()); > f2.displayFixtureDate(); > matchdate.add(Calendar.MONTH, -6); > } > // before getting next club(i), give two weeks space to avoid > having 2 matches on the same date > matchdate = today; > matchdate.add(Calendar.DATE, 14); > System.out.println("reset date to today, then added 14"); > } > printFixtureList(); > } > > > > > this is the system.out: > > [ league1.createFixtureList() ] > i=0 > > Liverpl vs Chelsea @ Anfield > > 05-Apr-2004 @ 15:00 > > Chelsea vs Liverpl @ chelseas ground > > 05-Oct-2004 @ 15:00 > > Liverpl vs Man Utd @ Anfield > > 12-Apr-2004 @ 15:00 > > Man Utd vs Liverpl @ Old Trafford > > 12-Oct-2004 @ 15:00 > > Liverpl vs Arsenal @ Anfield > > 19-Apr-2004 @ 15:00 > > Arsenal vs Liverpl @ Highbury > > 19-Oct-2004 @ 15:00 > > Liverpl vs Newcastle @ Anfield > > 26-Apr-2004 @ 15:00 > > Newcastle vs Liverpl @ St.James Park > > 26-Oct-2004 @ 15:00 > > reset date to today, then added 14 > > i=1 > > Chelsea vs Man Utd @ chelseas ground > > 19-Apr-2004 @ 15:00 > > Man Utd vs Chelsea @ Old Trafford > > 19-Oct-2004 @ 15:00 > > Chelsea vs Arsenal @ chelseas ground > > 26-Apr-2004 @ 15:00 > > Arsenal vs Chelsea @ Highbury > > 26-Oct-2004 @ 15:00 > > Chelsea vs Newcastle @ chelseas ground > > 03-May-2004 @ 15:00 > > Newcastle vs Chelsea @ St.James Park > > 03-Nov-2004 @ 15:00 > > reset date to today, then added 14 > > i=2 > > Man Utd vs Arsenal @ Old Trafford > > 24-May-2004 @ 15:00 > > Arsenal vs Man Utd @ Highbury > > 24-Nov-2004 @ 15:00 > > Man Utd vs Newcastle @ Old Trafford > > 31-May-2004 @ 15:00 > > Newcastle vs Man Utd @ St.James Park > > 30-Nov-2004 @ 15:00 > > reset date to today, then added 14 > > i=3 > > Arsenal vs Newcastle @ Highbury > > 20-Jun-2004 @ 15:00 > > Newcastle vs Arsenal @ St.James Park > > 20-Dec-2004 @ 15:00 > > reset date to today, then added 14 > > > > > No suprise that the issue happens when Arsenal is playng Man U matchdate = today hmmm.. aren't you then moving today when playing with matchdate afterwards? I would clone(). bruno |
|