0

Sometimes, due to faulty data collected, a line generated by the following method ends up looking like this when saved: ",-1,0" or something similar, with no name, an ID of -1 and a level of 115 or something else. (The lines are formatted like this (excluding quotes): "name,id,level" (e.g: "Honour guard,5514,115")

What i need to do is to remove all strings in monstersToAdd that contains -1. I've tried this, but with no success:

private void combineInfo() {
    for(int i = 0; i < monsterList.size(); i++){
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
    monstersToAdd.remove(monstersToAdd.contains("-1"));
}

with the line monstersToAdd.remove(monstersToAdd.contains("-1")); I was trying to remove all strings in monstersToAdd that contains "-1". This however does not work, probably for good reasons, which I unfortunately don't know of yet.

I would really appreciate any input :).

4
  • @Cold Hawaiian - doesn't seem like a homework (teachers rarely use monster games as example :), more like an enthusiastic attempt on a game software. Commented Jun 16, 2011 at 7:24
  • @Nick - but the asker can learn a lot if you give answer with a lecturing value Commented Jun 16, 2011 at 7:25
  • @ron not so sure -- check OP's other questions Commented Jun 16, 2011 at 7:29
  • 1
    I'm a bit surprised at how anyone could think of this as homework.. I realize the way I wrote the first lines might sound like a task for homework, but that was my way of formulating myself clearly. @nick I can't see how there is no learning value in this. If you'd looked properly at my other questions, you'd see how I've taken the answers and progressed far with them, also learning more things myself. Commented Jun 16, 2011 at 9:34

6 Answers 6

2

You would be better off not adding the lines you don't want in the first place.

for (....) {
  if (IDs[i] != -1) {
    // add it
  }
  // else it simply doesn't get added
}

More on your original code: You could post a little more detail, such as the type of monsterToAdd. If it is a non-generic list, then the contains method just returns true or false depending if the parameter (here a string of "-1") is present in the list exactly as you pass it, that is it doesn't search for substring matches of the list elements.

remove then tries to remove the element you ask to remove, which may be a Boolean object, automatically boxed from the boolean primitive value returned by contains.

Also, it is suspicious that you have a variable called monsterList which you use for iteration length, but not actually use any elements from that list. Maybe the arrays you use have the same values as the list, and were copied out beforehand? If so, it would be nicer to iterate on the monsterList directly and use its elements.

Sign up to request clarification or add additional context in comments.

3 Comments

IDs[i] might return a String since I don't see a IDs[i].toString() in his call to monstersToAdd.Add().
@j.w.r - Java automatically converts to String for the plus operator (if any op is a String). But yes, if it contains Strings, you need !IDs[i].equals("-1") in the condition
Thank you for the reply :). I must have been tired to think of this when I wrote it last night, seems so logical now, even though I had no idea how to do it last night. The variable called monsterList contains strings of information and the IDs, levels and names variables are variables that have drawn out some of the information from the monsterList variable. (just for explaining). Thank you guys once again :).
1

Its easier if you dont even add them, than adding and removing them so check the sanity of ID names and levels before adding them

private void combineInfo() {
for(int i = 0; i < monsterList.size(); i++){
    //add only if name is non empty, ID is not negative and level is below 100
    if(!(names[i].isEmpty() || IDs[i]<0 || levels[i]>100))
    monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
}

Comments

1

Why don't you do this instead:

private void combineInfo() {
for(int i = 0; i < monsterList.size(); i++){
    if(IDs[i] != -1){
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
}
monstersToAdd.remove(monstersToAdd.contains("-1"));

}

That way, you never add the monster to the list in the first place, if the ID is -1.

Comments

0

You are really close:

private void combineInfo() {
    for(int i = 0; i < monsterList.size(); i++){
        if (IDs[i] == -1) continue; // Skip this iteration
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
}

Filter them out as early as possible rather than back-tracking and removing them.

Comments

0

contains() returns only a true/false result depending on whether the list contains the given input object (in your case the string "-1"). So in your example, your list wouldn't contain "-1", so your remove statement would be resolved to this:

monstersToAdd.remove(false);

which wouldn't work for obvious reasons.

Comments

0

Here is the code:

for(Iterator<String> it = monsterList; it.hasNext();) {
    String elem = it.next();
    if (elem.contains("-1")) {
         it.remove();
    }
}

contains() method of collection returns true if collection contains element equals to one passed as an argument. In you case you want to use String's contains() that returns true if the string contains specified substring. This is the reason that you need loop. This loop must be implemented with iterator. Using new java 5 syntax for(String elem : list) will not work here because you have to remove element. Using for(int i = 0; i < list.size(); i++) requires implementation of logic that safely moves to the next index after element removal.

And the last point. You have to use iterator.remove() instead of Collection.remove() to avoid ConcurrentModificationException

1 Comment

In the original example, monsterList is not used just for determining the size. However your insight that it might have relevant data may be true, and caching out those data into separate arrays may not be necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.