The point of this program is to remove certain sports teams and their members from a text file, then overwrite the original file with the new set of values. This is attempted by reading the values into an array, then looping through the array and deleting the name of the team and the next 2 lines but for some reason it stops going through the array after the index.
I'm stuck, so any help would be great
Code:
private void RemoveSportsTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {
String ChosenTeam = "";
ChosenTeam = JOptionPane.showInputDialog("What Team Do you want to remove?");
ArrayList<String> Teamlist = new ArrayList<String>();
if (ChosenTeam.length() > 0) {
} else {
Scanner Reader = null;
try {
Reader = new Scanner(new File("ListofSportTeams.txt"));
} catch (FileNotFoundException ex) {
}
while (Reader.hasNext()) {
Teamlist.add(Reader.next());
}
Reader.close();
for (int count = 0; count < Teamlist.size(); count++) {
{
if (Teamlist.get(count).equals(ChosenTeam)) {
Teamlist.remove(count);
Teamlist.remove(count + 1);
Teamlist.remove(count + 2);
}
}
}
}
}
SportTeamList.txt =
Team1
Jeff
James
Team2
Steve
Peter
Now, I am using two lists. One is the original, the other is the removal list. I read the original list, if an entry starts with "Team" (please add if you have some other logic for differentiating team names from member names), I am adding it to the removal list and also the following entries until the next team name to be retained is found. Finally, I remove all the removal entries from the original list.
output
You should never
remove
from aList
when iterating. Which you are doing.Consider a trivial example, I have a list
{1,2,3,4,5}
. Let us assume its 0-indexed and I want to remove all numbers greater than 3.0 - List item is
1
, keep1 - List item is
2
, keep2 - List item is
3
, remove. All elements get shifted, list is now{1,2,4,5}
.3 - List item is
5
, remove4 - List item is oops, there is no longer a 4
So I have overshot the end of the
List
because I took the size to be5
when I started iterating but it became4
after I removed the element at index2
and it became3
when I removed the element at index3
.You might say, "Ah ha, I can fix this with a
while
loop":But this is even worse:
0 - List item is
1
, keep1 - List item is
2
, keep2 - List item is
3
, remove. All elements get shifted, list is now{1,2,4,5}
.3 - List item is
5
, removeSo, no error. Looks like the issue is fixed. But what does the list contain? It contains
{1,2,4}
. But4
is greater than3
. It was skipped due to the index shift. You now have an even more insidious bug.If you were using a proper enhanced foreach loop like so:
You would have, correctly, gotten a
ConcurrentModificationException
. This is just one of many reasons to use the enhanced foreach loop rather than looping by index.In order to do what you want use an
Iterator
:I will reiterate my comment:
Please always use Java naming conventions. Variables should always be in
camelCase
.PascalCase
is reserved for classes.UPDATE
It might be easier so use the
indexOf
method to find the team name in theList
and remove the required elementsIt is very important to remove the items in reverse. This is due to the same problem as above, if you remove the item at index (for example)
10
then the item that was at index11
is moved down. So when you go to remove the item at index11
you are actually removing the item what initially was it index12
.You can use a dirty trick
I.e. keep removing the item at the found index, as the list will shuffle downwards to fill the gap you will actually remove the required item and the two that were above it. I think this makes code very hard to read. You will probably forget this trick and then come back to the code and have to work out what it is doing.