Click to See Complete Forum and Search --> : Rugby League Table Array Swapping!!


marchbanks
11-20-2003, 07:50 AM
Have to create a league table which updates results. I have updated the results but am having diffuculty in updating the league table which is outputted to a file.

Any help on this is most apreciated, here is the code!

import java.io.*;
import java.util.*;

public class League {


private static final int MAX_TEAMS = 12;
private static final int RESULTS = 50;
private int noTeams;
private int noResults;
private RugbyTeam[] myMatch = new RugbyTeam[MAX_TEAMS];
private Results[] myMatch1 = new Results[RESULTS];

//Reads in League.txt
public void readMatch(){

String file = "League.txt";
String line;
noTeams = 0;

try{
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();

while ((line != null) && (noTeams <MAX_TEAMS)){

StringTokenizer st = new StringTokenizer(line,",");
myMatch[noTeams] = new RugbyTeam(st.nextToken(),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
line = inFile.readLine();
noTeams++;
}

inFile.close();
}

catch (IOException e){
System.out.println("Cannot open League file");
}
}
public void displayLeague(){
for (int i=0; i<noTeams; i++){
System.out.print("TEAM NAME: " +myMatch[ i] .getTName());
System.out.print(" NO OF MATCHES: " +myMatch[ i] .getTMatches());
System.out.println(" POINTS:"+myMatch[i].getTPoints());

}
}
//*****************************************************************************

//Reads in Results.txt
public void readResults(){

String file = "Results.txt";
String line;
noResults = 0;

try{
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();

while ((line != null) && (noResults <RESULTS)){

StringTokenizer st = new StringTokenizer(line,",");
myMatch1[noResults] = new Results(st.nextToken(),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),(st.nextTok en()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
line = inFile.readLine();
noResults++;
}

inFile.close();
}

catch (IOException e){
System.out.println("Cannot open Results file");
}
}
public void displaymyMatch1(){
//getHomePoints=score
for (int i=0; i<noResults; i++){
System.out.print("Home Team Name: "+myMatch1[i].getHomeName());
System.out.print(":Home Number Of Tries "+myMatch1[i].getHomeNoTries());
System.out.print(":Home Team Points "+myMatch1[i].getHomePoints());
System.out.print("Away Team Name: "+myMatch1[i].getAwayName());
System.out.print("Away Number Of Tries: "+myMatch1[i].getAwayNoTries());
System.out.println("\tAway Points: "+myMatch1[i].getAwayPoints());
}
}

//*************************************************************************



//Sorts teams in order of points
public void sortTeam(){
int min;
RugbyTeam temp = new RugbyTeam();

for (int i = 0; i < noTeams -1; i++ ){
min = i;
for (int j = i+ 1; j < noTeams;j++ ){
if (myMatch[ i].getTPoints()<myMatch[j].getTPoints()){
min = j;
}
}
temp = myMatch[min];
myMatch[min] = myMatch[i] ;
myMatch[i] = temp;
}
System.out.println("*** SORTED ****");
}

//*************************************************************************




//Writes team data to file
public void writeLeague(){
String file = "LeagueOut.txt";
String line;


try{
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter outfile=new PrintWriter(bw);

for (int i = 0; i < noTeams;i++ ){
outfile.println(""+myMatch[i].getTName()+","+myMatch[i].getTMatches()+","+myMatch[i].getTPoints());


}
outfile.close();
}

catch (IOException e){
System.out.println("Cannot open league file");

}
}

//finds teams

public RugbyTeam findRugbyTeam(String RugbyTeamTName){
RugbyTeam foundRugbyTeam = new RugbyTeam();

for (int i = 0; i<noTeams; i++){
if (RugbyTeamTName.equals(myMatch[i].getTName())){
foundRugbyTeam = myMatch[i];
break;
}
else{
foundRugbyTeam=null;
}
}
return foundRugbyTeam;
}


public void update()
{
//updates points for win/draw
for (int i=0; i<noResults; i++){

int home = 0;
int away = 0;




if (myMatch1[i].getHomePoints()>myMatch1[i].getAwayPoints())

{
home = home +4;

}

else if (myMatch1[i].getAwayPoints()>myMatch1[i].getHomePoints()){

away = away + 4;
}

else
{

home = home + 2;
away = away + 2;
}

//System.out.println("Name: "+"\t"+myMatch1[i].getHomeName()+"\t"+home+"Name: "+"\t"+myMatch1[i].getAwayName()+"\t"+away);

//updates 7 or less points

if ((myMatch1[i].getHomePoints()) - (myMatch1[i].getAwayPoints())>0&&(myMatch1[i].getHomePoints()) - (myMatch1[i].getAwayPoints())<=7)
{
away = away + 1;
}

else if ((myMatch1[i].getAwayPoints()) - (myMatch1[i].getHomePoints())>0&&(myMatch1[i].getAwayPoints()) - (myMatch1[i].getHomePoints())<=7)
{
home = home+1;
}
//System.out.println("Name: "+"\t"+myMatch1[i].getHomeName()+"\t"+home+"Name: "+"\t"+myMatch1[i].getAwayName()+"\t"+away);


//updates 4 tries or more

if (myMatch1[i].getHomeNoTries()>3)
{

home = home+1;
}

else if (myMatch1[i].getAwayNoTries()>3)
{

away = away+1;
}
System.out.println("Name: "+"\t"+myMatch1[i].getHomeName()+"\t"+home+"Name: "+"\t"+myMatch1[i].getAwayName()+"\t"+away);

//updates games played

//for (int i=0; i<noTeams; i++){
//int NoOfMatches;
//if (myMatch[i].getTMatches())
//NoOfMatches+1;
//public void Update2(byte[] buffer,
// int offset,
// int length)
}
}

}


public class Results {

private String Hname;
private int HnoTries;
private int Hpoints;
private String Aname;
private int AnoTries;
private int Apoints;


public Results(String hn, int ht, int hp, String an, int at, int ap){
Hname = hn;
HnoTries = ht;
Hpoints = hp;
Aname = an;
AnoTries = at;
Apoints = ap;


}

public Results(){
}
//get methods home
public String getHomeName(){
return Hname;
}
public int getHomeNoTries(){
return HnoTries;
}
public int getHomePoints(){
return Hpoints;
}
//get methods away
public String getAwayName(){
return Aname;
}
public int getAwayNoTries(){
return AnoTries;
}
public int getAwayPoints(){
return Apoints;
}

//set methods home
public void setHomeName(String HN){
Hname = HN;
}
public void setHomeNoTries(int HT){
HnoTries = HT;
}
public void setHomePoints(int HP){
Hpoints = HP;

//set methods away
}
public void setAwayName(String AN){
Aname = AN;
}
public void setAwayNoTries(int AT){
AnoTries = AT;
}
public void setAwayPoints(int AP){
Apoints = AP;
}

}
public class RugbyTeam {



//public Team homeT = new Team();
//public Team awayT = new Team();

private String TeamName;
private int NoOfMatches;
private int Points;

public RugbyTeam (String N, int M, int P){
TeamName = N;
NoOfMatches = M;
Points = P;
}

public RugbyTeam(){
}
public String getTName(){
return TeamName;
}

public int getTMatches(){
return NoOfMatches;
}

public int getTPoints(){
return Points;
}

}

import java.io.* ;


public class TeamTest {

public static void main (String args[] ) throws IOException {

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr) ;

//League locatedTeam = new League();

RugbyTeam locatedRugbyTeam = new RugbyTeam();
String TeamName;

League premier = new League();
premier.readMatch();
premier.readResults();
premier.displaymyMatch1();
premier.displayLeague();
premier.sortTeam();
premier.displayLeague();

premier.update();
premier.writeLeague();

System.out.print("Type Team Name= ");
TeamName = stdin.readLine();

locatedRugbyTeam = premier.findRugbyTeam(TeamName);

if (locatedRugbyTeam == null){
System.out.println("Team not found");
}

else{
System.out.print("Team Name Found= "+locatedRugbyTeam.getTName());
}

}
}

fredmv
11-20-2003, 07:59 AM
That is Java code, this is a JavaScript forum. They are two entirely different languages. Try posting at the Sun Developer Network Forums (http://forum.java.sun.com/).