Relative newbie here.
I am trying to count occurrences of objects in a J2EE web application. Very simple.
I have an Oracle database, with a table called Warnings. Each warning can have one of three statuses:
- ACTIVE
- AUTOCLEARED
- CLEARED
I have a WARNING_SUPPRESSED and a WARNING_SUPERSEDED column in my table. The values these columns hold is then used to determine the state of a warning. There is no warning_status column, so the status of the warning is worked out as follows:
NB - I would like to point out that I currently have 5 warning objects, one of which is cleared. So, I have 4 active and 1 cleared warning.
I have some logic in the servlet:
for (Warning warning : warnings)
{
// logic to check and count occurrences of each type of warning
int activeCount = 0;
int clearedCount = 0;
int autoclearedCount = 0;
// warning is active if it is not superseded and not suppressed
if( ((warning.getWarningSuperseded() != null && warning.getWarningSuperseded().equals("N"))
&& ((warning.getWarningSuppressed() != null && warning.getWarningSuppressed().equals("N")))))
{
activeCount++;
}
// warning is cleared if it is superseded
if(warning.getWarningSuperseded() != null && warning.getWarningSuperseded().equals("Y"))
{
clearedCount++;
}
// warning is autocleared if it is suppressed
else if (warning.getWarningSuppressed() != null && warning.getWarningSuppressed().equals("Y"))
{
autoclearedCount++;
}
}
So, what I think I am trying to do here is, in pseudo code:
if warning not superseded and warning not suppressed
then increment activeCount;
if warning is superseded
then increment clearedCount;
if warning is suppressed
then increment autoclearedCount;
When debugging this in Eclipse, I get activeCount to be 8, clearedCount to be 3 and autoclearedCount to be 1. I don't quite get why this is, because as far as I am concerned, I only have 5 warning objects, 4 active and 1 cleared.
What I am then doing, just to check that I have the amount of warnings I think I have:
warningCount = warningServices.getWarningCount(MyServletHelper.getCurrentSchema(request));
This returns 4 - the method getWarningCount() only returns a count of warnings that are active.
I can only assume there's some kind of flaw in my logic here - I don't know if it's just because I have had a long day or what, but this doesn't make sense to me!
Sometimes all it takes is for a fresh pair of eyes to see what I am doing wrong...
adding to gvo's answer: Tom, you should use continue operator in if block or you could use if-else-elseif properly. ie:
change your code accordingly and let us know the result.
[edited] The code you wrote...technically program control could go in to two if block is a pass.