CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    1

    Unhappy 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);
    }
    }
    }


    }

  2. #2
    Join Date
    Apr 2007
    Posts
    425

    Re: help constructing an array

    Read this:

    http://www.codeguru.com/forum/showthread.php?t=217745

    and try again.

    Think of this from my perspective.

    I come to read this post, and i see [you] post a message which describes a word problem about creating a league table based on the results, followed by a bunch of un-formatted code. This is where i stopped reading.

    1) You haven't bothered to read the submission guidelines, and have put the most minimal effort in trying to offer any information to someone who you are hoping to help you with no form of repayment other than a digital 'thank you'.

    2) I don't know what a league table is, and i don't care what a league table is. I don't care about the results either. This has nothing to do with Java, and is irrelevant to your real problem which you haven't described.
    Last edited by Deliverance; December 18th, 2009 at 04:04 PM.
    ------
    If you are satisfied with the responses, add to the user's rep!

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: help constructing an array

    Bluntly put, but fair comment - I stopped reading just where you did.

    The OP didn't even ask a question...

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Dec 2009
    Posts
    4

    Re: help constructing an array

    To help fair to him the title of his post is "help constructing an array," but he shouldn't have tried to get someone to solve his homework for him. No one is going to go through the whole program for you dude!

    Constructing an array is a fundamental question you should already know when attempting to write this program. You can always google up java array.

    http://java.sun.com/docs/books/tutor...ts/arrays.html

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help constructing an array

    If the OP needs help constructing an array then who wrote this line of code?
    Code:
    Table [] teams = new Table[(lineCounter.getLineNumber()*2)];

  6. #6
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: help constructing an array

    If the OP needs help constructing an array then who wrote this line of code?

    Table [] teams = new Table[(lineCounter.getLineNumber()*2)];
    That was Voodoo magic. Copy/paste artifacts.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured