-1
public void addNewLamp (Lamp newLamp)
{
    for(Lamp lampName: lamps)
    {
        if(lampName.equals(newLamp.getLampName()))
        {
            System.out.println("no");
            Break;
        }
        else
        {
            System.out.println("New Lamp!");
            lamps.add(newLamp);
        }
    }
}

lamps being an array of type Lamp.

When looking at the debugger it seems like the for loop just skips everything, the entire IF statement, howcome?

EDIT: Code has been edited to fit comments, still same problem.

This is not the same question as the one that was linked to me since its more than just comparing strings, I've now changed != to "if(!lampName.getLampName().equals(newLamp.getLampName()))" and still getting the same result. Thanks for the link though

user3650211
  • 3
  • 1
  • 7
  • 2
    In addition to the problem that you are trying to compare strings using `!=`, you should *not* be trying to modify `lamps` at the same time you have an iterator going through it. The result will be a `ConcurrentModificationException`. – ajb Nov 20 '14 at 15:51
  • 1
    Besides, you might want to add the newLamp only once, not one time for each lamp in your list... – AJPerez Nov 20 '14 at 15:54
  • 1
    Just use a `Map`. – Boris the Spider Nov 20 '14 at 15:55
  • Thanks for all the answers! Cant use Map since its for a school project and thats a few chapters ahead. – user3650211 Nov 20 '14 at 15:56
  • you can use `break` after lamp addition, supporting @AJPerez comment – Aramillo Nov 20 '14 at 15:59
  • In addition to all the other problems, your logic is flawed. It looks like you want to add a new lamp if it doesn't already exist in the list. Instead, your logic adds `newLamp` if it is unequal to **any** item in the list. That is, the only time it doesn't add it is if all the lamps in the list have the same name! If you step through your logic carefully, by hand, one statement at a time, to see what your program does, I think you will see the problem. – ajb Nov 20 '14 at 15:59
  • @Aramillo that won't work until he fixes his logic which is backwards. – ajb Nov 20 '14 at 16:00
  • If you change the code and are still having a problem, and you can't figure it out, you should post a new question since it's a different question. – ajb Nov 20 '14 at 16:04

1 Answers1

1

I assume lampName is a String? If so you need to cmpare them like this:

if(!lampName.getLampName().equals(newLamp.getLampName()))

EDIT: as ajb mentioned in his comment: if you want to iterate through the list and you modify it (by adding elements into it) you need to use Iterator:

Iterator<Lamp> lampIterator = lamps.iterator();

while(lampIterator.hasNext()) {
   if(lampIterator.next().equals(newLamp.getLampName()))
        {
            System.out.println("no");
            break;
        }
        else
        {
            System.out.println("New Lamp!");
            lamps.add(newLamp);
        }
}

also break keyword is all lowercase

Lucas
  • 3,181
  • 4
  • 26
  • 45