-2

I dont know why, but whenever I Choose Done in my do while loop it doesn't terminate.

public class testClinic{ 
      public static void main(String[]args){
        Clinic c=new Clinic();
        do{ 
            c.details(JOptionPane.showInputDialog("[P]roceed","\n[D]one"));
        }while(!c.details().equals("D"));
}
}
  • 1
    Please provide the code for `Clinic.details(String)`. Note that the `details` method and `details` field are not the same thing. – Andy Turner Jul 23 '16 at 22:47
  • Please provide code that compiles (this one has unbalanced parentheses at the end). – Sergey Kalinichenko Jul 23 '16 at 22:51
  • I would suggest to write code that does not depend on hidden details in Clinic class. The code might work, but will be hard to maintain. – Roland Jul 23 '16 at 23:07

1 Answers1

1

I will try to answer your question, but you really don't provide enough detail about your code for me to be sure my answer will help you.

My best guess is that your Clinic class looks something like this:

public class Clinic {
    public String details;

    public void details(String input) {
        details = input;
    }
}

The first thing I notice is that your call to JOptionPane.showInputDialog does not appear to be doing what you want. Again, I'm only guessing as to what you want based on what little information is provided. I'm thinking you want a dialog that asks the user for one of two inputs - either P for "Proceed" or D for "Done". If you look at the Javadoc for JoptionPane you will see that your code is invoking this method which is causing your dialog to appear like this:

enter image description here

Now, if you simply click OK to this dialog and your Clinic class is similar to the one I show above, then the details field is set to " [D]one" which, obviously, is not equal to "D" and therefore your loop never ends.

If you change the line that calls the details method to this:

c.details(JOptionPane.showInputDialog("[P]roceed\n[D]one","D"));

You will get what you want - the prompt will look like this:

enter image description here

and when you press OK the loop will end.

Some things to consider regarding your code:

  1. The call to JOptionPane.showInputDialog returns the text entered by the user. If the user presses Cancel you will get a java.lang.NullPointerException when the loop attempts to access c.details (again, assuming Clinic works as I outlined above)
  2. In general you should always write your equals comparison using the constant first and the variable second so that you avoid any possibility of NullPointerException. For example in your code: }while(!c.details.equals("D")); would change to }while(!"D".equals(c.details));
  3. You should generally never make the fields of a class public. Giving direct access to the internal data of objects can make your code harder to maintain (as @Roland mentioned in the comments). Instead use getter and setter methods. The accepted answer to this question explains this in more detail.
Community
  • 1
  • 1
D.B.
  • 4,523
  • 2
  • 19
  • 39