3

I am using a foreach loop to run through the arraylist and compare the name to a string. But I have no idea why when I compare a the name to a string it always prints out.

for (Picture item : collection) {


                System.out.println("This is the label " + item.getName());

                if (item.getName().equals("This shouldn't work")); {

                System.out.println("Why is this working");

                }
            }
        }

Output

getting the name test A 
This is the label A
getting the name test A
Why is this working
getting the name test B
This is the label B
getting the name test B
Why is this working
jpIII
  • 77
  • 6
  • 7
    You have a dangling `;` after your `if` condition. – Sotirios Delimanolis Oct 15 '13 at 15:11
  • @SotiriosDelimanolis the common semicolon usage/non-usage problem. – Luiggi Mendoza Oct 15 '13 at 15:12
  • And there goes the 'duh' face-palm ... quick survey, how many hours did you lose to this one, @pplll, and will you do that again? (Oh, for me it was about 2, and never done it again).... – rolfl Oct 15 '13 at 15:14
  • This is off topic, I think (too localized), but it could also be closed as a duplicate of [semicolon at end of if statement](http://stackoverflow.com/q/14112515/1281433). – Joshua Taylor Oct 15 '13 at 15:21
  • @JoshuaTaylor: I think the "Too Localized" flag was removed. – Colin DeClue Oct 15 '13 at 15:34
  • @ColinDeClue It was, but we can still type it into the custom close reasons (and I did). It's still useful to mention in the comment, though, to help indicate to other users what sort of problem the question has. I think most typo-type problems are too localized. I do wish I had seen the possible duplicate first, though. – Joshua Taylor Oct 15 '13 at 15:35

3 Answers3

4

Semicolons indicate the end of a statement, which is a component of a block. By typing

if (condition);
{ 
  System.out.println("Why is this working");
}

you are indicating

if (condition)
  // empty statement
;
{ // unconditional opening of a block scope
  System.out.println("Why is this working");
}

So, if your if statement evaluates true, nothing will happen, and if it evaluates false, then the empty statement will be skipped, which is the equivalent to nothing happening.

Now if you have removed that semicolon, then the next "statement" would be an opening of a block-scope:

if (condition) { 
  // conditional opening of a block scope
  System.out.println("Why is this working");
}

and you would have seen the expected behavior, skipping "Why is this working" as output when the condition is false.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 1
    Situations like this are almost enough to wish for an excplicit "empty statement" keyword, which would be required for all empty statements. `if (condition) empty_stmt` – Edwin Buck Oct 15 '13 at 15:26
1

if (item.getName().equals("This shouldn't work")); //here is semicolon present

Your code should like below

if (item.getName().equals("This shouldn't work")){

 }
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

Change

if (item.getName().equals("This shouldn't work")); {

to

if (item.getName().equals("This shouldn't work")) {

if you put the semicolon the if statement ends

Cirou
  • 1,420
  • 12
  • 18