Solved

# Java : find smallest difference

Posted on 2012-08-27
1,354 Views
Hi Experts,
----------------------------------------------------------------------------------------------------------------
The file football.txt contains the results from the English Premier League for 2001/2. The columns labeled ‘F’ and ‘A’ contain the total number of goals scored for and against each team in that season (so Arsenal scored 79 goals against opponents, and had 36 goals scored against them). Need a java program to print the name of the team with the smallest difference in ‘for’ and ‘against’ goals.
-------------------------------------------------------------------------------------------------------------
My main objective of this question is to identify the activities we will be able to write the code and demonstrate some of the practices and techniques that we would use in best practices under industry. I'm mainly be looking for the thought process and how we can complete this task. But points goes to the first/best post.

Thanks a lot Experts.
0
Question by:Dushan911

LVL 26

Expert Comment

0

LVL 17

Author Comment

Not really, this is actually a task assigned for to me by my project manager.
0

LVL 16

Assisted Solution

Hmm. Well, anyway, if you are not concerned with multiple teams having the same goal differences, then you can just store the differences in a Vector using add(int index, Object o), where 'index' is the goal difference.
0

LVL 17

Author Comment

Thanks a lot! Could you please provide working code? I'm actually collecting diffident algorithms with using different languages.
0

LVL 16

Expert Comment

I'm mainly be looking for the thought process and

...and that's mainly what I posted. (If your post was directed at me that is).
0

LVL 17

Author Comment

ok cool.. I will try to do it myself :).
0

LVL 13

Assisted Solution

I would use the same logic whether I did it in Java, VB.NET, C++,C# or even Excel.

Read each line, parse the values, calculate the difference, store the teamname and difference . When done, sort on difference ascending and return all the teams with until the difference value changed.

A slightly better approach (wouldn't work in Excel, but would in VBA) would be to store the current minimum as you parse lines, and if the current difference is less than current minimum, replace the old and store the new teamname. Last remaining teamname wins!
If there's a chance of ties, simply use an array/arraylist rather than a string.
0

LVL 15

Expert Comment

.gp.
0

LVL 16

Expert Comment

``````import java.util.Vector;

class ScoreDiff {

public static Vector<TeamResult> sArray;
public static Vector<TeamResult> scoreArray;
private static TeamResult[] tr = new TeamResult[20];
protected static int mx;

public static void main(String[] args){

ScoreDiff sd = new ScoreDiff();

tr[0] = new TeamResult("Arsenal",12,24);
tr[1] =new TeamResult("Spurs",2,14);
tr[2] =new TeamResult("Man City",6,9);
tr[3] =new TeamResult("Man United",22,89);
tr[5] =new TeamResult("West Ham",45,89);
tr[6] =new TeamResult("Fulham",22,2);
tr[7] =new TeamResult("Everton",22,209);
tr[8] =new TeamResult("Swansea",22,38);
tr[9] =new TeamResult("Chelsea",22,122);
tr[10] =new TeamResult("Liverpool",922,189);
tr[11] =new TeamResult("West Brom",722,839);
tr[12] =new TeamResult("Southampton",2222,869);
tr[13] =new TeamResult("Aston Villa",72,99);
tr[14] =new TeamResult("Stoke",12,64);
tr[15] =new TeamResult("Newcastle",64,90);
tr[16] =new TeamResult("Norwich",11,779);
tr[17] =new TeamResult("QPR",52,290);
tr[18] =new TeamResult("Sunderland",65,56);
tr[19] =new TeamResult("Wigan",17,44);

scoreArray = new Vector<TeamResult>(mx);
sArray = new Vector<TeamResult>();

for(int y = 0;y<mx;y++){
}

scoreArray.ensureCapacity(mx);

for(int u=0;u<tr.length;u++){

}

Enumeration en = sd.scoreArray.elements();
TeamResult temp;

while(en.hasMoreElements()){
temp = (TeamResult)en.nextElement();

if(!(temp.teamName.equals("Dummy"))){

}

}

for (int k=0;k<sd.sArray.size();k++){

System.out.println(((TeamResult)sd.sArray.elementAt(k)).goalDiff);

}

}

public static class TeamResult{

protected String teamName;
protected int goalsScored;
protected int goalsAgainst;
protected int goalDiff;

public TeamResult(String tN, int goalsIn, int goalsOut){
this.teamName = tN;
this.goalsScored = goalsIn;
this.goalsAgainst = goalsOut;
this.goalDiff = java.lang.Math.abs(goalsIn-goalsOut);
if(java.lang.Math.max(goalsIn,goalsOut)>mx){mx = java.lang.Math.max(goalsIn,goalsOut);}
}

}

}
``````
0

LVL 17

Author Comment

Thanks a lot all for your details!
Hi Krakotoa,
I want to pick data from the text file.
0

LVL 16

Accepted Solution

Well, you'll have to implement that part yourself; you'll know how to read in a text file and parse the entries I'm pretty sure. ;)

With the code I posted above, there is an issue that someone more forensic than I will have to comment on if they come along : 3 of the results are not ordered properly in the sample print out - 27,26,27. This should of course be 26,27,27, but for some reason it isn't. The following code however does work, as a colleague pointed out to me:

``````import java.util.*;

public class ScoreDiff {
public static void main(String[] args) {
TeamResult[] tr = new TeamResult[20];

tr[0] = new TeamResult("Arsenal", 12, 24);
tr[1] = new TeamResult("Spurs", 2, 14);
tr[2] = new TeamResult("Man City", 6, 9);
tr[3] = new TeamResult("Man United", 22, 89);
tr[4] = new TeamResult("Reading", 3200, 89);
tr[5] = new TeamResult("West Ham", 45, 89);
tr[6] = new TeamResult("Fulham", 22, 2);
tr[7] = new TeamResult("Everton", 22, 209);
tr[8] = new TeamResult("Swansea", 22, 38);
tr[9] = new TeamResult("Chelsea", 22, 122);
tr[10] = new TeamResult("Liverpool", 922, 189);
tr[11] = new TeamResult("West Brom", 722, 839);
tr[12] = new TeamResult("Southampton", 2222, 869);
tr[13] = new TeamResult("Aston Villa", 72, 99);
tr[14] = new TeamResult("Stoke", 12, 64);
tr[15] = new TeamResult("Newcastle", 64, 90);
tr[16] = new TeamResult("Norwich", 11, 779);
tr[17] = new TeamResult("QPR", 52, 290);
tr[18] = new TeamResult("Sunderland", 65, 56);
tr[19] = new TeamResult("Wigan", 17, 44);

Arrays.sort(tr);

for (int i = 0; i < tr.length; i++) {
System.out.println(tr[i]);
}
}

public static class TeamResult implements Comparable<TeamResult> {
protected String teamName;
protected int goalsScored;
protected int goalsAgainst;
protected int goalDiff;

public TeamResult(String tN, int goalsIn, int goalsOut) {
this.teamName = tN;
this.goalsScored = goalsIn;
this.goalsAgainst = goalsOut;
this.goalDiff = java.lang.Math.abs(goalsIn - goalsOut);
}

@Override
public int compareTo(TeamResult other) {
return goalDiff - other.goalDiff;
}

public String toString() {
return String.format("name=%s,diff=%d", teamName, goalDiff);
}
}
}
``````
0

LVL 17

Author Comment

Hi Thanks a lot krakatoa and other experts for your effort,
I want to take lot more time and should compare different approaches bit later.

I'm bit on hurry and bit having time issue with other tasks..
I'm really apologizing for asking this due to my available less time..
Could you please provide the java code to read and display it from text file?
0

LVL 17

Author Closing Comment

Thanks a lot!
0

LVL 16

Expert Comment

ok.
0

## Featured Post

### Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop: