|
-
December 18th, 2009, 12:39 PM
#1
help constructing an array
hi, i have an assugnment where i need to read in football results from a text file output, total statistics, (games played & goals scored)output stats for a specific team, and then create a league table based on the results. the only task left is the league table. i have tried and tried to do this but i just cant get it to work it outputs every result to a new line of the array, even if its a team that has played before.
heres what ive done so far:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.FileInputStream;
public class html8
{
public static class Table
{
String team_name;
int team_goals_for,
team_goals_against,
team_points;
Table(String n, int gf, int ga, int pts) {
team_name = n;
team_goals_for = gf;
team_goals_against = ga;
team_points = pts;
}
}
public static void main(String[] args)
{
//Declare and initialise variables
int
goals = 0,
match_count = 0,
invalid_count = 0,
games_played = 0,
games_won = 0,
games_lost = 0,
games_drawn = 0,
goals_for = 0,
goals_against = 0,
home_score = 0,
away_score = 0,
count = 0,
away_points = 0,
home_points = 0;
String
nextLine = null,
teamstats ="",
home_team,
away_team,
match,
answer,
answer2,
yes = "yes";
Scanner team = new Scanner(System.in);
Scanner yes_no = new Scanner(System.in);
Scanner html = new Scanner(System.in);
//line counter to see how many lines the text document has
LineNumberReader lineCounter = null;
try
{
lineCounter = new LineNumberReader( new InputStreamReader(new FileInputStream("results2.txt")));
} catch (FileNotFoundException e2)
{
e2.printStackTrace();
}
try {
while ((nextLine = lineCounter.readLine()) != null) {
if (nextLine == null)
break;
}}
catch (Exception done) {
done.printStackTrace();
}
Table [] teams = new Table[(lineCounter.getLineNumber()*2)];
System.out.println("would you like to view statistics for a specfic team (please answer yes or no)");
answer = yes_no.nextLine();
if (answer .equalsIgnoreCase(yes))
{
System.out.println("which team would you like to view statistics on?");
//Get user input for selected team statistics
teamstats = team.nextLine();
}
File file = new File("results2.txt");
//create html page
Writer output = null;{
try {
output = new BufferedWriter(new FileWriter("results.html"));
output.write("<table border = 2 cellspacing=2 cellpadding=5>");
output.write("<br>");
output.write("<br>");
output.write("<table border = 3 cellspacing=2 cellpadding=5>");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext())
{
String result = scanner.nextLine();
final String [] next = result.split(":");
if (next.length == 4)
{
match_count++;
home_team = next[0].trim();
away_team = next[1].trim();
try{
home_score = (Integer.parseInt(next[2].trim()));
away_score = (Integer.parseInt(next[3].trim()));
if (home_score > away_score)
{
home_points = 3;
away_points = 0;
}
else if (home_score == away_score)
{
home_points = 1;
away_points = 1;
}
else
{
home_points = 0;
away_points = 3;
}
goals = goals +(home_score+away_score);
match = home_team +" ["+home_score+ "] | "+away_team+" ["+away_score+"]";
try {
teams[count] = new Table(home_team,home_score,away_score,home_points );
teams[count+1] = new Table(away_team,away_score,home_score,away_points );
System.out.println(match);
count=count+2;
}catch (java.lang.ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
if (teamstats.equalsIgnoreCase(home_team)) {
games_played++;
goals_against = goals_against + away_score;
goals_for = goals_for + home_score;
if (away_score > home_score)
games_lost++;
else if (away_score < home_score)
games_won++;
else if (away_score == home_score)
games_drawn++;
}
if (teamstats.equalsIgnoreCase(away_team)) {
games_played++;
goals_against = goals_against + home_score;
goals_for = goals_for + away_score;
if (home_score > away_score)
games_lost++;
else if (home_score < away_score)
games_won++;
else if (home_score == away_score)
games_drawn++;
}
try {
String newline = System.getProperty("line.separator");
output.write("<tr><td ALIGN = center>"
+ home_team.toUpperCase() + "<td>" + home_score + "<td>"
+ away_team.toUpperCase() + "<td>" + away_score
+ "</td></tr>");
output.write(newline);
} catch (IOException e) {
e.printStackTrace();
}
}
catch (NumberFormatException nfe)
{
System.err.println("NumberFormatException: " + nfe.getMessage());
}
}
else
{
invalid_count++;
}
//*******************
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Arrays.sort(teams, new Comparator<Table>() { public int compare(Table t1, Table t2) { return new Integer(t1.team_points).compareTo(t2.team_points); } } );
System.out.println(" ");
System.out.println("League table GF GA PTS");
for (int index = 0; index < teams.length; index++ )
{
System.out.println(teams[index].team_name + " : " + teams[index].team_goals_for+ " : " + teams[index].team_goals_against + " : " + teams[index].team_points);
}
System.out.println("");
System.out.println("Todays Statistics;");
System.out.println("");
System.out.println("The total number of goals today was "+goals);
System.out.println("The total number of valid matches today was "+match_count);
System.out.println("The total number of invalid matches today was "+invalid_count);
// if answered yes to specific team output
if (answer .equalsIgnoreCase(yes))
{
System.out.println("");
System.out.println("Statistics for "+teamstats);
System.out.println("");
System.out.println("Games Played : "+ games_played);
System.out.println("Games won : " + games_won);
System.out.println("Games lost : " + games_lost);
System.out.println("Games drawn : " + games_drawn);
System.out.println("Goals For : " + goals_for);
System.out.println("Goals Against : " + goals_against);
System.out.println("League table");
};
try {
output.write("<table border = 4 cellspacing=2 cellpadding=5>");
output.write("<br>");
output.write("<tr><td ALIGN = center>" + "Summary" + "<td>"
+ "Totals");
output.write("<tr><td ALIGN = center>"
+ "The amount of goals today was: " + "<td>" + goals
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "The amount of games today was: " + "<td>" + match_count + "</br>");
output.write("<tr><td ALIGN = center>"
+ "The amount of invalid games played today was:" + "<td>"
+ invalid_count + "</br>");
if (answer .equalsIgnoreCase(yes))
{
output.write("</table>");
output.write("<table border = 4 cellspacing=2 cellpadding=5>");
output.write("<br>");
output.write("<tr><td ALIGN = center>"+"Statistics for: <td>" + teamstats );
output.write("<tr><td ALIGN = center>"
+ "Games Played : " + "<td>" + games_played
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "Games won : " + "<td>" + games_won
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "Games lost : " + "<td>" + games_lost
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "Games draw : " + "<td>" + games_drawn
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "Goals for : " + "<td>" + goals_for
+ "</br>");
output.write("<tr><td ALIGN = center>"
+ "Goals against : " + "<td>" + goals_against
+ "</br>");
}
output.close ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out
.println("would you like to generate your results in html?");
answer2 = html.nextLine(); // /// Scans the user input //////
if (answer2 .equalsIgnoreCase(yes)){
String fileName = "results.html"; // name of the file to be opened
// Exec a command to the OS.
try {
Runtime.getRuntime().exec("cmd.exe /C start " + fileName);
} catch (IOException e) {
// Could not open the file, show message to use
System.err.println("Error: Unable to open " + fileName);
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|