Below is part of some code I have written. I am trying to see whether nodes in a network are connected, and thus finding 'failurepoints'. I have a vector containing node Ids which I loop through to check for connections using a hasPath method. This all works fine, but my problem is with the output. I want to indicate to a user whether or not a node they selected (earlier in the program) is a failure point, by outputting some dialog. However when I run the program, the JOptionPane message panel keeps reappearing multiple times (I'm guessing 'i' number of times) and I can't get rid of it without pressing ok or the 'x' button multiple times (a problem if I get a huge network, lots of button presses required!). Can anyone see what I can do to make the panel appear only once? Thanks in advance.

for (int i = 1; i< cnodes.size();i++){

		boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);


                    boolean OneKid = false;
                    if (cnodes.size() < 2) {
                      OneKid = true;
                    }
                    boolean FailurePoint;

                    
                     
                    if ((isConnected == true)|| (OneKid == true)) {
                        FailurePoint = false;
                        
                        JOptionPane.showMessageDialog(null,
                                "The selected node is not a failure point.",
                                "Result", JOptionPane.INFORMATION_MESSAGE);

                    }
                    
                    else if (isConnected == false){
                            FailurePoint = true;
                        
                        JOptionPane.showMessageDialog(null,
                                "The selected node is a failure point.",
                                "Result", JOptionPane.INFORMATION_MESSAGE);

                    }
                    
					else if ((isConnected !=false) && (isConnected !=true)){
                    
                    JOptionPane.showMessageDialog(null,
                            "The selected node is not a failure point.",
                            "Result", JOptionPane.INFORMATION_MESSAGE);}
                }
}

Recommended Answers

All 8 Replies

huuuh...., please what are you tied with

#else if ((isConnected !=false) && (isConnected !=true)){

you can test (if - if else - else) boolean value directly

if(isConnected){
   if(OneKid){
      //some stuff
   }
   if(!OneKid){
      //some reversal stuff
   }
   //some stuff
}else{
  //some stuff
}

Move the messages out of the loop:
Initialse failurePoint to false, set it to true if/when you find a failure point (do not set it back to false inside the loop!), the after exiting the loop test the boolean to see which Message to display.

Thanks for replying. If I move if statements and the messages out of the loop the isConnected parameter is not recognized any more.
Also, I've just realized I have another problem.....I actually need to set FailurePoint as true if any of the isConnected values = false i.e. any of i iterations of isConnected comes out as false, but if all of iConnected values are true then FailurePoint = false. I tried to put the iconnected values into a vector, then see if the vector contains a false value, but I get errors as follows:

for (int i = 1; i < cnodes.size(); i++) {

                    boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);


                    Vector isConnectedVec = new Vector();
                    isConnectedVec.add(isConnected);



                    for (int ii = 0; ii < isConnectedVec.size(); ii++) {
                        Object result = isConnectedVec.elementAt(ii); //here it makes me set result as an object, it won't allow boolean even though the elements in isConnected are booleans

                        boolean OneKid = false;
                        if (cnodes.size() < 2) {
                            OneKid = true;
                        }
                        boolean FailurePoint = false;

                        if (OneKid == true) {
                            FailurePoint = false;
                        }


                        if (result == false) {  // error here: this can't be used as a boolean as it was initialized as an object, but I want to know if there is a false value in isConnectedVec!
                            {
                                FailurePoint = true;
                            }
                        }


                        if (FailurePoint == true) {
                            JOptionPane.showMessageDialog(null,
                                    "The selected node is a failure point.",
                                    "Result", JOptionPane.INFORMATION_MESSAGE);
                        }

                        if (FailurePoint == false) {
                            JOptionPane.showMessageDialog(null,
                                    "The selected node is not a failure point.",
                                    "Result", JOptionPane.INFORMATION_MESSAGE);
                        }



                    }

                }

I'm a programming beginner, so please forgive any naivety or stupid elementary mistakes I'm probably making!

No, I never suggested moving the if tests out of the loop, just the messages

If I do that, like this, then the FailurePoint parameter is not recognized. I know I'm probably doing something really elementary wrong, but I just can't spot it...Sorry if I'm not understanding you properly!

for (int i = 1; i < cnodes.size(); i++) {

                    boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);



                   Vector isConnectedVec = new Vector();
                    isConnectedVec.add(isConnected);



                    boolean OneKid = false;
                    if (cnodes.size() < 2) {
                        OneKid = true;
                    }
                    boolean FailurePoint = false;


                    if (OneKid == true) {
                        FailurePoint = false;
                    }


                    if (isConnectedVec.contains(false)) // if (isConnected == false){
                    {
                        FailurePoint = true;
                    }

                }


                    if (FailurePoint == true) {
                        JOptionPane.showMessageDialog(null,
                                "The selected node is a failure point.",
                                "Result", JOptionPane.INFORMATION_MESSAGE);
                    }

                    if (FailurePoint == false) {
                        JOptionPane.showMessageDialog(null,
                                "The selected node is not a failure point.",
                                "Result", JOptionPane.INFORMATION_MESSAGE);
                    }

                



        }
    }

Scope problem. Declare failurePoint at a higher context:

boolean FailurePoint = false;

for (int i = 1; i < cnodes.size(); i++) {

//code ...
}

Line 7 -you create a new vector every time you iterate thru the loop, which goes out of scope when the loop exits. Try creating the Vector before entering the loop.

Thank you all for your help, my program is working now. I only just joined up yesterday, but I have to say, this forum is amazing. I can't believe how helpful you all are. Thanks very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.