0

I'm developing a program and in it, a field from a database is pulled and according to its numerical value, will display one of three things in the GUI: "WARNING", "SEVERE" OR "CRITICAL".

If it is between 0 and 100, it should display "WARNING" If it is between 100 and 200, it should display "SEVERE" If it is over 200, it should display "CRITICAL"

The part of my code that determines this is posted below. I am getting unfavorable results, for any value that is between 100 and 200, "ERROR" is displayed. Is my logic wrong, or is there something deeper going on here?

public class ThreatPanel {

...

final int TEST = 0;
final int TEST2 = 100;
final int TEST3 = 200;
...
}



public void ShowThreats(){

    String targetEnd = MainDisplay.getTargetIpHolder();     
    TargetServerData.setText(targetEnd);

    String attackerEnd = MainDisplay.getAttackerIpHolder();
    AttackerData.setText(attackerEnd);

    int threatLevelEnd = MainDisplay.getThreatLevelHolder();
    System.out.println(threatLevelEnd);
    if ((threatLevelEnd > TEST ) && (threatLevelEnd < TEST2)){
        ThreatLevelData.setText("WARNING");
    }
    if ((threatLevelEnd > TEST2 ) && (threatLevelEnd < TEST3)){
        ThreatLevelData.setText("SEVERE");
    }
    if (threatLevelEnd > TEST3){
        ThreatLevelData.setText("CRITICAL");
    }
    else{
        ThreatLevelData.setText("ERROR");
    }

}
exit_1
  • 1,240
  • 4
  • 13
  • 32
  • 1
    Where is 100, 200 defined? I mean which variable? – Smit Jul 03 '13 at 23:37
  • Im guessing at TEST, TEST2 and TEST3. . . . in an unseen part of the code – jsedano Jul 03 '13 at 23:37
  • @Smit it is defined as a class variable. ill update code – exit_1 Jul 03 '13 at 23:38
  • 1
    @Pat I have one more question and if you answer it could be answer to your question. For `Warning`, is `100` inclusive, What I mean is should it stop at `99` or at `100`? and same goes for others. and for this better use `else - if` – Smit Jul 03 '13 at 23:43
  • @Smit I can't say for sure yet, it depends on my partner's decision who isn't available at the moment. But for now, let's say Warning should be from 1 to 99 including 99, Severe should be from 100 to 199. – exit_1 Jul 03 '13 at 23:50
  • 1
    @Smit changed the second and third if statements to else if statements and I got favorable results. Thank-you. Could you perhaps explain why this is so? – exit_1 Jul 03 '13 at 23:56

2 Answers2

2

Solution to your problem:

// checks for value in between 0 to 100 excluding 0 and 100
if (threatLevelEnd  > 0 && i<100) 
    System.out.println("WARNING");
// checks for value in between 100 to 200 excluding 200
else if (threatLevelEnd >= 100 && threatLevelEnd < 200) 
    System.out.println("SEVERE");
// checks for value greater than 200
else if (threatLevelEnd >= 200)
    System.out.println("CRITICAL");
else 
// this is default if value is negative or zero
     System.out.println("ERROR");

Currently what you are doing.

// checks for value in between 0 to 100 excluding 0 and 100
if (threatLevelEnd  > 0 && i<100) 
    System.out.println("WARNING");
// checks for value in between 100 to 200 excluding 100 and 200
if (threatLevelEnd > 100 && threatLevelEnd < 200) 
    System.out.println("SEVERE");
// checks for value greater than 200
if (threatLevelEnd > 200)
    System.out.println("CRITICAL");
else 
// if value is not grether than 200
     System.out.println("ERROR");

So anyhow your very last if-else is getting executed and overwriting your previous values.

Smit
  • 4,685
  • 1
  • 24
  • 28
1

Your last else statement only applies to the if directly above it, so this else statement will always trigger when threatLevelEnd is less than TEST3, overwriting any values set in your first 2 if clauses (where you set warning and severe).

Using an if and else if idiom will avoid this, since then the later if clauses are only executed if the earlier if did not, so the final else clause will not override earlier settings.

Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40