Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how to write an if statement which evaluates 3 dialogue boxes

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default how to write an if statement which evaluates 3 dialogue boxes

    Hi all

    Im trying to write a code that has a for loop which iterates through 3 instance variables, the 3 instance variables are each set using a confirm dialogue box so if the user clicks yes they ae set with the value true and if the user clicks no they are set wit the value false. I have done this and it works fine,

    however the next step is to display an alert message if less than 2 of the dialogue boxes evaluate to true. I am extremely stuck on this and ive been working on it for hours now. im thinking I will use an if statement but I dont know how to evaluate the 3 instance variables. I know how to work with one but im not even sure if its possible to evaluate 3 and have the condition evaluate to true if less tan 2 of theinstance variables evaluate to true?

    I hope someone can help as im really stuck on this now.

    just to clarify, what im trying to do is write a loop following these guidelines:

    if less than 2 of the dialogue boxes result in true
    do something

    or it may be possible to do it like this??:

    if less than 2 of the instance variables are equal to true
    do something

    this doesent have to be a loop if there is another way to do it, i just think a loop is the way I need to do it, however as i am a noob to programming I could ofcourse be wrong!

    sorry for not adding code but I am currently moving house so have no internet connection for my pc. I am posting these messages via a mobile phone.

    Thanks in advance to anyone who attempts to help me
    Last edited by humdinger; January 7th, 2010 at 03:53 AM. Reason: spelling mistake in title


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    You can count how many are checked....quite a few ways to do this. Here's two methods (there are more than this though):
    Using a loop
    int x = 0;
    for ( int i = 0; i < 3; i++ ){
         if ( value[i] ){ x++; }
    }
    if ( x < 2 ){
    //do something
    }else if ( x == 2 ){
    //2 are checked, do something else
    }else{
    //all are checked, do something else
    }

    In a single statement:

    if ( ( value[0] && value[1] && !value[2] ) ||
    		( value[0] && !value[1] && value[2] ) ||
    			( !value[0] && value[1] && value[2] ) ){
    	//only 2 are checked
    }

    All code assuming value is an array containing your booleans

  3. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (January 8th, 2010)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    hi copeg, thankyou for the reply, i worked this out in the end last night. I did it the same as your second example & ive tested it & it all works. thankyou again for the assistance

  5. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    Could you also do this without the array. for exemple by just checking
    if ( x==1)
                {
                 //do something
                }
    etc etc??

    also,

    how then would you get the dialog boxes to ask a question (to get the true/false)?? where would you store these true false boolean answers?

    Am i making sense?
    Last edited by helloworld922; January 13th, 2010 at 05:37 PM.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    Yes, it can be done without arrays, but you can't compare booleans with integers in Java.
    boolean val1 = true;
    if (val1 == 1) // Syntax error
    However, you will still need 3 variables to put the values in. Copeg chose an array because that's one of the easiest ways to store a list of variables.

    There are two main ways to get a dialog box: make it yourself, or use the JOptionPane class.

    JOptionPane.showConfirmDialog(null,"Do you confirm?");

    Note that showConfirmDialog actually returns an integer value, so you have to compare it to the static response values stored in the JOptionPane class:
    if (JOptionPane.showConfirmDialog(null,"Do you confirm") == JOptionPane.YESOPTION)
    {
        // yes selected
    }

  7. #6
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    Ah right I'm with you!!

    The only thing is, in the course that I'm doing, we haven't yet learnt about arrays so will not be able to use them.

    If I put the answers into 3 variables, how would I then check to see if 2 or more of the variables do contain a 'true'?

    could I do something that checks "do you want yes or no" - then if yes is chosen it adds 1 to a value. This would be repeated 3 times and then if the value is 2 or more then something is executed?

    I'm sure I'll figure this out but some input would help.

    thanks so much (in advance).

    John

  8. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to write an if statement which evaluates 3 dialogue boxes

    No worries - I've just cracked it!!

    Basically I craeted another varaiable and each time added to to it. All's good

    Thanks

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 03:33 PM
  2. write object to sql db
    By wolfgar in forum JDBC & Databases
    Replies: 3
    Last Post: May 18th, 2010, 10:03 AM
  3. How can i write this in code?
    By Mr. steve in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2009, 09:07 PM
  4. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 06:16 PM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM