Question

There's a fair bit of code involved and I'm not sure how much detail to go into, but I'm populating a treemap with data from a mysql table and I'm having trouble iterating through it.

Here's the code for the class containing the treemap

public class SeasonResults{

private static Map<String,SeasonResults> allResults = new TreeMap<String,SeasonResults>(); 
private String hometeam;
private String awayteam;
private String result;                                          

private static SeasonResults result6;

public static SeasonResults add(String hometeam, String awayteam, String result) 
{
        result6 = new SeasonResults(hometeam, awayteam, result);
        allResults.put(hometeam,result6);

    return result6;
}


private  SeasonResults(String hometeam, String awayteam, String result) 
{
    this.hometeam = hometeam;
    this.awayteam = awayteam;
    this.result = result;
}

public static Collection<SeasonResults> getCollection() 
{
    return allResults.values();
}

@Override
public String toString() 
{ 
    return " "+hometeam+", "+awayteam+", "+result; 
}

}

And here's the code where I populate the array and then try and iterate through it.

public void HeadToHead(){
    try 
    {
        //Sets up the connedtion to the database and installs drivers which are required.
        Class.forName("com.mysql.jdbc.Driver");                                                                        
        con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");        

        String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?";
        PreparedStatement prepst;            

        prepst =  con.prepareStatement(SQL);
        prepst.setString(1,box1.getSelectedItem().toString());
        prepst.setString(2,box2.getSelectedItem().toString());
        rs = prepst.executeQuery();

        while (rs.next()) 
        {
            //This retrieves each row of League table and adds it to an array in the League Results class.

            hometeam = rs.getString("HomeTeam");
            awayteam = rs.getString("AwayTeam");                    
            result = rs.getString("Result");                    

            custs = (hometeam + "," + awayteam + "," + result);     // Takes all the variables containging a single customers information and puts it into a string, seperated by commas.
            SeasonResults.add(hometeam, awayteam, result);
        }
    }
    catch (Exception e) 
    {
        System.out.println("Error " +e);                                                                               
    }

    Seasonrecord = SeasonResults.getCollection();
    seasons = new SeasonResults[Seasonrecord.size()];
    Iterator iterateSeason = Seasonrecord.iterator(); 
    int i = 0;

    while(iterateSeason.hasNext()){
        seasons[i] = (SeasonResults)iterateSeason.next(); 
        i++;

        if(result.equals("HW")){
            hometeamvalue = hometeamvalue + 50;
        }  
        else if(result.equals("D")){
            hometeamvalue = hometeamvalue + 10;
            awayteamvalue = awayteamvalue + 10;
        }
        else{
            if(result.equals("AW")){
                awayteamvalue = awayteamvalue + 50;
            }
        }
    }
}

There are 5 'result' fields in the database. 2 are 'HW', 2 are 'AW', and 1 is 'D'. What I'm trying to do is print out 'hometeamvalue' and 'awayteamvalue' but when I do the value is printed as only 10. Only the first field's value is used.

I use the same code to iterate through the array when I want to display the results in a GUI, and all the fields are shown. But when I try and do some calculations with them, it doesn't work.

Any ideas what the problem is?

Was it helpful?

Solution

You have to do like this

SeasonResults.java

public class SeasonResults{
private String hometeam;
private String awayteam;
private String result;                                          

public String getHometeam() {
    return hometeam;
}
public void setHometeam(String hometeam) {
    this.hometeam = hometeam;
}
public String getAwayteam() {
    return awayteam;
}
public void setAwayteam(String awayteam) {
    this.awayteam = awayteam;
}
public String getResult() {
    return result;
}
public void setResult(String result) {
    this.result = result;
}

public  SeasonResults(String hometeam, String awayteam, String result) 
{
    this.hometeam = hometeam;
    this.awayteam = awayteam;
    this.result = result;
}
@Override
public String toString() 
{ 
    return " "+hometeam+", "+awayteam+", "+result; 
}

}

HeadToHaed method

public void HeadToHead(){
        String hometeam,awayteam,result;
        int hometeamvalue,awayteamvalue;
          List<SeasonResults> allResults = new ArrayList<SeasonResults>();

        try 
        {
            //Sets up the connedtion to the database and installs drivers which are required.
            Class.forName("com.mysql.jdbc.Driver");                                                                        
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");        

            String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?";
            PreparedStatement prepst;            

             prepst =  con.prepareStatement(SQL);
            prepst.setString(1,box1.getSelectedItem().toString());
            prepst.setString(2,box2.getSelectedItem().toString());
            ResultSet rs = prepst.executeQuery();

            SeasonResults seasonResults=null;
            while (rs.next()) 
            {

                //This retrieves each row of League table and adds it to an array in the League Results class.

                hometeam = rs.getString("HomeTeam");
                awayteam = rs.getString("AwayTeam");                    
                result = rs.getString("Result");                    

                seasonResults=new  SeasonResults( hometeam,  awayteam,  result) ;
                custs = (hometeam + "," + awayteam + "," + result);     // Takes all the variables containging a single customers information and puts it into a string, seperated by commas.
                allResults.add(seasonResults);
            }
        }
        catch (Exception e) 
        {
            System.out.println("Error " +e);                                                                               
        }

        System.out.println("SIze of ArrayList::"+allResults.size());
        for(SeasonResults temp:allResults)
        {
               if(temp.getResult().equals("HW")){
                    hometeamvalue = hometeamvalue + 50;
                }  
                else if(temp.getResult().equals("D")){
                    hometeamvalue = hometeamvalue + 10;
                    awayteamvalue = awayteamvalue + 10;
                }
                else{
                    if(temp.getResult().equals("AW")){
                        awayteamvalue = awayteamvalue + 50;
                    }
                }

        }

    }

Let Me Know If U Face Any Issues

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top