Results 1 to 7 of 7
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default If statement executing when false...

    I'm drawing a blank, this loop/if statement is executing it's contents even when it's false.

    Here is the snippet:
    Java Code:
    	for (int i = 1; i < strActiveSteps.length; i++)
    	{
    		if (strActiveSteps[i].equals("1") && strStepsToShow == null)
    		{
    			strStepsToShow = "(status='" + strActiveStepsHdrs[i] + "'";
    		}
    		else if (strActiveSteps[i].equals("1") && !strStepsToShow.equals(""));
    		{
    			//System.out.println("Passed the test with " + strActiveSteps[i] + " which resulted in " + strActiveSteps.equals("1") + " and " + !strStepsToShow.equals(""));
    			System.out.println("This must be truetrue " + strActiveSteps[i].equals("1") + !strStepsToShow.equals(""));
    			strStepsToShow = strStepsToShow + " OR status='" + strActiveStepsHdrs[i] + "'";
    		}
    	}
    Here are some results so you can see that it is executing the println even when false

    Java Code:
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue falsetrue
    This must be truetrue falsetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue falsetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue falsetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    This must be truetrue falsetrue
    This must be truetrue truetrue
    This must be truetrue truetrue
    I'm sure that I'm making some completely obvious mistake but I'm not aware of it. I am guessing it has something to do with the fact that I am looking for an integer value in a string but I wouldn't thank that would be a problem.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Print what you are testing. i.e print out exactly what's in your if test.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ribbs2521 View Post
    Java Code:
    		else if (strActiveSteps[i].equals("1") && !strStepsToShow.equals(""));
    What's that semicolon doing there at the end of the line?

    kind regards,

    Jos

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    headDesk++ ;

  5. #5
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    headDesk++ ;
    May want to loop that a few times in my case.:o

    Thanks josAH, I've made that mistake before but with methods and it generates a compile error, this one didn't generate compile or run time errors.

  6. #6
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by ribbs2521 View Post
    May want to loop that a few times in my case.:o

    Thanks josAH, I've made that mistake before but with methods and it generates a compile error, this one didn't generate compile or run time errors.
    That is because you can have an if statement (else, while, for, etc.) with an empty block. The semi-colon indiciates that the statement does not have anything following it. This useful for when you have a loop that you simply need to spin.

    Here's an example used for when writing concurrent programs:

    Java Code:
    AtomicInteger num = new AtomicInteger(3);
    while (!num.compareAndSet(3, 4))
        ;
    This loop will spin until compareAndSet() returns true.

  7. #7
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    That is pretty interesting. I never knew that. Of course, there are a lot of things I don't know but that's what I'm here (in the forum) for. Thanks.

Similar Threads

  1. Replies: 4
    Last Post: 08-11-2009, 12:31 PM
  2. If statement not executing correctly
    By gligor_kot in forum New To Java
    Replies: 5
    Last Post: 08-03-2009, 02:46 AM
  3. Replies: 2
    Last Post: 07-18-2008, 02:13 PM
  4. hibernate lazy=False
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 08:54 PM
  5. Replies: 4
    Last Post: 05-19-2007, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •