Java Polava

Discussion in 'Software' started by Pitsweat, Aug 23, 2004.

  1. Pitsweat

    Pitsweat Private E-2

    I'm hoping that someone could be so kind as to point me in the right direction with a problem I am having dealing with StringTokenizer, FileReader and BufferedReader classes to do with a Java project I've kindly been delegated.

    The below code is intended to read from a text file to populate a League table, feeding off a class called Team (that I almost believe I have working but haven't inluded below). Having torn what little hair I have left out I thought it wise to ask those who actually know what they are doing. Any help would be appreciated but by all means feel free to point and laugh.

    Many thanks,

    Pit.

    ............................

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

    class League
    {
    private static Team teams[] = new Team[6];
    League()
    {
    String tName;
    String tNickname;
    int nPlayed;
    int nWon;
    int nLost;
    int nDrawn;
    try
    {
    FileReader fr = new FileReader("Results.txt");
    BufferedReader br = new BufferedReader(fr.readLine, 1);
    int i = 0;
    while(i < 5)
    {
    StringTokenizer st = new StringTokenizer(br);
    while(st.hasMoreTokens())
    {
    tName = st.nextToken();
    tNickname = st.nextToken();
    nPlayed = st.nextToken();
    nWon = st.nextToken();
    nLost = st.nextToken();
    nDrawn = st.nextToken();
    teams = //New Team Instance TO BE WRITTEN
    }
    i++;
    }
    fr.close();
    br.close();
    }
    catch (Exception e)
    {
    System.err.println("Caught IOException: "+ e.getMessage());
    }
    }
    public int getSize()
    {
    // To BE WRITTEN
    }
    }
    public Team getTeam(int i)
    {
    // TO BE WRITTEN
    }
    }
     
  2. Wookie

    Wookie Sergeant Major

    I can try to help, would be easier than trying to figure out what problem you are having by myself if you told me exactly what error or whatever you are getting.
     
  3. Pitsweat

    Pitsweat Private E-2

    Cheers Wookie, should have put all the details in last post but was having a panic at the time, no sleep and lots of coffee seem to have gotten rid of that panic.

    I think I have worked out the problem with the fileReader, stringTokenizer but the one thing I have been tearing my hair out over is to sort out the details. I have included all my code this time and a sample text file which is to be read. The who idea of the program is to try and create an instance of "League" which contains many(6 in this case) instances of "Team" which have been read from a text file. These Teams should be printed firstly on the screen (so that I know it has been read correctly - this may be deleted later) and then sorted into points order before printing in points order. I can't get any form of code to correctly sort this out. Compare using Arrays.sort() keeps bringing up the message that it doesn't know what Arrays is.

    Anyway, if you could advise, or fancy what I hope is a bit of a challenge (otherwise I really am just clueless) then feel free. I will be presenting this option to my manager tomorrow in any case as we're trying to sort data out for our sports and social Sports Day report... the things I do to stop using Excel...

    I'll thank you now, to save jumbling code up later :)

    //Team class
    class Team
    {
    private String name;
    private String nickname;
    private int played;
    private int won;
    private int drawn;
    private int lost;
    private int points;

    Team(String nm, String nick, int pld, int wn, int drw, int lst)
    {
    name = nm;
    nickname = nick;
    played = pld;
    won = wn;
    drawn = drw;
    lost = lst;
    points = (won*3) + drawn;
    }
    public int getPoints()
    {
    return points;
    }

    String getName()
    {
    return name;
    }

    public String getNickname()
    {
    return nickname;
    }

    public int getPlayed()
    {
    return played;
    }

    public int getWon()
    {
    return won;
    }

    public int getDrawn()
    {
    return drawn;
    }

    public int getLost()
    {
    return lost;
    }
    public int compareTo(Object object)
    {
    return(((Team)this).name).compareTo(((Team)object).name);
    }
    }


    ///////////////////////////////////////////////////////////////////////////////////////
    //League class

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

    class League
    {
    private static Team teams[] = new Team[6];
    League()
    {
    String tName;
    String tNickname;
    int nPlayed;
    int nWon;
    int nLost;
    int nDrawn;
    try
    {
    FileReader fr = new FileReader("Results.txt");
    BufferedReader br = new BufferedReader(fr, 1);
    int i = 0;
    while(i < 6)
    {
    StringTokenizer st = new StringTokenizer(br.readLine());
    while(st.hasMoreTokens())
    {
    tName = st.nextToken();
    tNickname = st.nextToken();
    nPlayed = Integer.parseInt(st.nextToken());
    nWon = Integer.parseInt(st.nextToken());
    nLost = Integer.parseInt(st.nextToken());
    nDrawn = Integer.parseInt(st.nextToken());
    teams = new Team(tName, tNickname, nPlayed, nWon, nLost, nDrawn);
    }
    i++;
    }
    fr.close();
    br.close();
    }
    catch (Exception e)
    {
    System.err.println("Caught IOException: "+ e.getMessage());
    }
    }
    public int getSize ()
    {
    return teams.length;
    }

    public Team getTeam(int i)
    {
    return teams;
    }


    }

    //////////////////////////////////////////////////////////////////////////////////////
    //League Main

    import avi.*;


    public class LeagueMain
    {
    public static void main(String args[])
    {
    League unnLeague = new League();// Define window and a league

    Window win = new Window("Ladies Hockey League", "plain", "black", 12);// Write window headings
    win.showWindow();
    win.write("\nPrint Teams Unsorted as read from file.\n\n");
    win.write("\nTeam Name\t\t"+"Nickname\t"+"Played\t"+"Won\t"+"Drawn\t"+"Lost\t"+"Points\t");



    //process each Team's details


    for(int i = 0; i < unnLeague.getSize(); i++)
    {
    //process and print out the League Teams

    Team temp = unnLeague.getTeam(i);
    win.write("\n\n"+temp.getName()+"\t\t"+temp.getNickname()+"\t"+temp.getPlayed()+
    "\t"+temp.getWon()+"\t"+temp.getDrawn()+"\t"+temp.getLost()+"\t"+temp.getPoints()+"\n");

    }

    //Print out sorted Team array details
    //Print out report footer

    }
    }

    /////////////////////////////////////////////////////////////////////////////////////
    //Results.txt to drop into same directory as project

    BillyMill Mashers 6 3 2 1
    ChirtonGrange Gnashers 6 0 4 2
    Heaton Hackers 6 4 1 1
    Walker Wanderers 6 2 2 2
    Wallsend Wranglers 6 1 4 1
    WhitleyBay Bombers 6 4 0 2
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds