Results 1 to 8 of 8
  1. #1
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default If-statement does not filter correctly

    Hi guys. I am literally looking for more than an hour on a simple if-statement I wrote. It should filter elements from a List, but it does not filter anything, although some elements satisfy all constraints. I am slightly frustrated now and I hope you can help me find what is going wrong.

    When I exclude this piece of code from my if-statement
    Java Code:
    && (D.timeLastStatusChange+ D.timeToCompany) > (timeOfTrigger + diversionBound)
    in the following if-statement

    Java Code:
    				if (D.driverStatus.equals("Going to company") /*&& (D.timeLastStatusChange+ D.timeToCompany) > (timeOfTrigger + diversionBound) */&&
    						D.getDriverZone().zoneName.equals(triggeredZone.zoneName) && (timeOfTrigger-D.timeLastStatusChange) != 0)	
    				{ 
    					System.out.println(D.driverStatus + " " + D.getDriverZone().zoneName+ " " + triggeredZone.zoneName + " " + D.timeLastStatusChange + " " +
     D.timeToCompany + " " + timeOfTrigger + " " + diversionBound);
    
    					listOfAvailableDrivers.add(D);
    				}
    The printed text for one particular element is then:
    Going to company WEST WEST 1086 6 1088 0

    When I include this piece of code, I get nothing. Why? To me, the statement should be true: (1086 + 6)> (1088 + 0)

    Changing it to
    Java Code:
    ((D.timeLastStatusChange+ D.timeToCompany) - (timeOfTrigger + diversionBound) >0)
    or something similar does not work too.
    Last edited by casta; 06-30-2016 at 03:34 PM.

  2. #2
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: If-statement does not filter correctly

    I just found out that is the way the if-statement adds the two numbers: 01086
    How do I solve this?

    All values are int-values

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: If-statement does not filter correctly

    Try debugging the code by adding some println statements that print out the values of the variables that are used in the if statements so you can see what their values are to understand why the code executes as it does. Also print out the values of the expressions. For example all of these:
    (D.timeLastStatusChange + D.timeToCompany)
    (timeOfTrigger + diversionBound)

    (D.timeLastStatusChange+ D.timeToCompany) - (timeOfTrigger + diversionBound)
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: If-statement does not filter correctly

    Thank you for your comment.
    All three calculations work out well.
    For one element with values 975 12 980 0
    I get
    987
    980
    7
    When I include it in my if-statement, it does not work.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: If-statement does not filter correctly

    When I include it in my if-statement, it does not work.
    What is the value of the expression used in the if statement? Can you print it?
    Also post the full source of if statement that the expression is in.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: If-statement does not filter correctly

    The full method looks like this:
    Java Code:
    	private static void createListOfAvailableDrivers() {
    		for (Driver D : listOfDrivers) {
    			if (D.driverStatus.equals("STANDBY") && D.getDriverZone().zoneName.equals(triggeredZone.zoneName) ) 
    			{ 
    				listOfAvailableDrivers.add(D);	
    			}
    			else {}
    			
    			if (diversionAllowed)
    			{
    				if ((D.timeLastStatusChange+ D.timeToCompany) - (timeOfTrigger + diversionBound) > 0)
    				{ 
    					System.out.println(D.driverStatus + " " + D.getDriverZone().zoneName+ " " + triggeredZone.zoneName + " " + D.timeLastStatusChange +
     " " + D.timeToCompany + " " + timeOfTrigger + " " + diversionBound + " optelsom " + D.timeToCompany + D.timeLastStatusChange);
    					System.out.println((D.timeLastStatusChange + D.timeToCompany));
    					System.out.println(timeOfTrigger + diversionBound);
    					System.out.println((D.timeLastStatusChange+ D.timeToCompany) - (timeOfTrigger + diversionBound));
    					listOfAvailableDrivers.add(D);
    				}
    			} 
    		} 
    	}
    I shortened the if-statement. Still does not provide satisfactory results.

    Again, when removing this particular formula in my if-statement, I get prints like the following:

    Going to company APR APR 959 28 959 0 optelsom 28959

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: If-statement does not filter correctly

    I see 3 if statements. Which one are you talking about? Is it the one on line 11?
    Note: The println statements should be executed before the if statements, not inside of them.
    Move the print statements on line 15 to 17 to after line 10.

    What is the value of:
    (D.timeLastStatusChange+ D.timeToCompany) - (timeOfTrigger + diversionBound)
    That is the value that is compared to 0

    There are 3 print outs shown in post #4 but the code in post#6 has 4 print statements????
    You make it harder to understand what is happening when you do not show all of what is printed.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: If-statement does not filter correctly

    Are you expecting this bit:
    28959

    to be this:
    987
    ? (that is, the sum, and not a concatenation)

    If so then:
    Java Code:
    public class TestStuff {
    
        public static void main(String args[]) {
            System.out.println("concat =  " + 1 + 2 + " summed = " + (1 + 2));
        }
    }
    you need brackets.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 9
    Last Post: 02-05-2012, 11:02 PM
  3. Replies: 4
    Last Post: 02-25-2011, 02:28 PM
  4. If statement not executing correctly
    By gligor_kot in forum New To Java
    Replies: 5
    Last Post: 08-03-2009, 01:46 AM
  5. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 11:36 AM

Posting Permissions

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