Results 1 to 3 of 3

Thread: [SOLVED] If's

  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] If's

    :confused:

    I have put together a piece of code

    if
    else

    The first three parts work fine, however once we get passed the first three

    Java Code:
    if ((outputBmi < 18.5) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                        } else {
    
                       if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                           txtResults.setText("   OK");
                                           txtResults.setForeground(Color.GREEN);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                           txtResults.setText("   Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
    It all starts to run amuck

    by purely changing the f to an F I can get a totally differant reading in the BMI.......

    Like I say all the if's are layed out exactly the same, I cannot see the error.

    Hopefully someone can point it out to me.


    Thanks in advance.

    Java Code:
    if ((outputBmi < 18.5) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                        } else {
    
                       if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                           txtResults.setText("   OK");
                                           txtResults.setForeground(Color.GREEN);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                           txtResults.setText("   Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE); // check bmi readings
                                txtResults.setBackground(Color.BLACK);
                        } else {
    
                       if ((outputBmi <= 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                           txtResults.setText("   OK");
                                           txtResults.setForeground(Color.GREEN);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi > 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                           txtResults.setText("   Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                          }
                       if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                        } else {
    
                       if ((outputBmi <= 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                           txtResults.setText("   OK");
                                           txtResults.setForeground(Color.GREEN);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi > 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                           txtResults.setText("   Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                        } else {
    
                       if ((outputBmi <= 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                           txtResults.setText("   OK");
                                           txtResults.setForeground(Color.GREEN);
                                           txtResults.setBackground(Color.BLACK);
                          } else {
    
                       if ((outputBmi > 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                           txtResults.setText("   Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                          }
            }
                        
                          }
                          }}}}}}}}}

  2. #2
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    It seems condition given by you is not correct.
    I think you want to check
    BMI && (gender should be F || f) && age

    So every condition can be changed as follows

    Java Code:
    if ((outputBmi <= 23) && ((aGender.equals("F")) || (aGender.equals("f"))) && (age <= 19))
    Also seems you missed an else in between the code

    Java Code:
    txtResults.setBackground(Color.BLACK);
                          } [B]--->else{[/B]
                       if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
    Hope this helps you.

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Wink

    Thanks, the fact that I had missed the else statement out was the issue, everything is working up to now on testing.

    Java Code:
    if ((outputBmi < 18.5) && ((aGender.equals("F" )) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
            } else {
    
            if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
            } else {
    
            if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
            } else {
    
            if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE); // check bmi readings
                                txtResults.setBackground(Color.BLACK);
            } else {
    
            if ((outputBmi <= 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
            } else {
    
            if ((outputBmi > 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
            } else {
            if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else {
    
             if ((outputBmi <= 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else {
    
             if ((outputBmi > 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
             } else {
    
             if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else {
    
             if ((outputBmi <= 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else {
    
             if ((outputBmi > 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
                          }
            }
    Thanks again, always pays to have another set of eyes

    lol

    :D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •