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

    Question [SOLVED] if's & else's again

    I thought this was solved,

    I have a number of if's I am trying to get working,

    you can either enter a 'f' or a 'F' for female in the gender field, it seems I have put the code together incorrectly because the result of ok underweight or overweight changes depending on which f I use.

    The suggestion made of ( f || F ) does not validate

    The code I have is

    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);
                          }
            }
    Can someone point out what I am doing wrong please because despite the lower case being second in the statement that is the one giving the correct result so question

    does the way I have put this together mean that the second agender f is overriding the first aGender F?

    If so how can i cure this?

    Thanks in advance

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

    Default

    Also all test carried out on the first 3 if's for the <19 yr old are working fine in both lower and upper case f F's

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

    Question

    No matter what I do if i enter

    f for female and enter the weight and height then click calculate it gives me correct readings

    but

    if I change the lower f to F and click calculate, it doesnt change the bmi reading of say 23.5 but it changes the result so it could have said 'ok' for the lower case f but when I sawp it for a F then the result changes from ok to overweight.

    starting with the 29 year old if's

    Java Code:
    } 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.7) && ((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 {

    I cannot see where the differance is from the 19 yr old!

    Can someone give me a clue please

    signed

    Clueless

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I think you have you're parenthesis misplaced... Is this the logic that you're looking for? (example):
    Java Code:
    if ((outputBmi > 26.8) && (aGender.equals("F") || aGender.equals("f")) && (age <= 49))
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    I have already posted in your previous post to change ur if statement.
    For your reference

    Java Code:
    if ((outputBmi <= 23) && ((aGender.equals("F")) || (aGender.equals("f"))) && (age <= 19))
    Please recheck my reply for your previous post.

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

    Default

    CJSLMAN, thanks , this time I will run several tests on each before i close this thread.....lol.....but thanks again

    Kbindumadhavi, yes I replied to your previous post, the missing else was great cos I would not have noticed it for hours

    However the suggestion of ( f || F ) does not validate

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

    Default

    I tested the validation and it is working fine.
    That is the reason I replied again with the same solution.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Another solution to the "F" and "f" dilema is to use the String equalsIgnoreCase() method:

    String equalsIgnoreCase (Java Platform SE 6))

    This way, you don't have to worry about the case of the string.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default SomeTimes Think in Enums

    Yes, the ignoreCase thing was also my first idea :) A total different solution would be introducing enum constants. So you can cancel out your string in overall. This is usually a construction for things that don't really change, which are
    Java Code:
    public enum SexNotWhatYouMightThink {
    FEMALE("F"), MALE("M");
    //private constructor here
    //private string member here
    }
    
    //outside code:
    {
    SexNotWhatYouMightThink a = SexNotWhatYouMightThink.MALE;
    }
    Alternatively you can keep your String for mappings or what not. Especially when you read from outside java (database, url parameters) things will always come down to mappings. So perhaps this might also be interesting:

    Java Code:
    public enum SexNotWhatYouMightThink2 {
    FEMALE("F"), MALE("M");
    //private constructor here
    //private string member here
    }
    Cheers
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

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

    Question Radio buttons

    :confused:

    I have 2 radio buttons one for metric an one for imperial, i have found that although I have all the code in place, the metric radio works however when i selected the imperial and entered the figures I was very suprised by the answer then found that it was calculating off of the metric formula.

    I have cm and kg's

    and inches and lbs

    Java Code:
    if (jRmetric.isSelected()==true){
         // try catch for numeric data only
                    try {
                         cm = Double.parseDouble(txtHeight.getText());//expect any numerical input
                        }
                      catch (NumberFormatException n)  {//to catch none numeric input
                        optPane1.showMessageDialog(this,"Enter numbers only please",
                        "please re-enter",optPane1.ERROR_MESSAGE); // Optpane kicks in on error
                        ok = false; //boolean to trigger Optpane on alpha entry
                        }
    // start of formula to convert cm to inches and kg's to lb's
                    cm=Double.parseDouble(txtHeight.getText());
                    outputInches = clCm2Inch.FormulaCmtoInch(cm);
                    txtConvHeight.setText(outinch=bodymass.format(outputInches));                              
    // End of cm to inch conversion formula               
                    // Start of weight conversions
                    // convert kgs to lbs
                    try {
                         kgs = Double.parseDouble(txtWeight.getText());//expect any numerical input
                        }
                        catch (NumberFormatException n)  {//to catch none numeric input
                            optPane1.showMessageDialog(this,"Enter numbers only please",
                            "please re-enter",optPane1.ERROR_MESSAGE); // Optpane kicks in on error
                            ok = false; //boolean to trigger Optpane on alpha entry
                        }
                         kgs=Double.parseDouble(txtWeight.getText());
                         outputlbs = clKg2Lbs.FormulaKgtoLbs(kgs);
                         txtConvWeight.setText (outlbs=bodymass.format(outputlbs));
                        } 
                        // End of kgs to lbs conversion formula
    and

    Java Code:
    if (jRimp.isSelected()==true){
                    try {
                        inch = Double.parseDouble(txtHeight.getText());//expect any numerical input
                        }
                         catch (NumberFormatException n)  {//to catch none numeric input
                          optPane1.showMessageDialog(this,"Enter numbers only please",
                          "please re-enter",optPane1.ERROR_MESSAGE); // Optpane kicks in on error
                          ok = false; //boolean to trigger Optpane on alpha entry
                        }
                          inch=Double.parseDouble(txtHeight.getText());
                          outputCms = clInch2Cm.Inch2Cm(inch);
                          txtConvHeight.setText(outcms=bodymass.format(outputCms));
                    // lbs to kgs conversion
                    try {
                         lbs = Double.parseDouble(txtWeight.getText());//expect any numerical input
                        }
                        catch (NumberFormatException n)  {//to catch none numeric input
                         optPane1.showMessageDialog(this,"Enter numbers only please",
                         "please re-enter",optPane1.ERROR_MESSAGE); // Optpane kicks in on error
                         ok = false; //boolean to trigger Optpane on alpha entry
                        }
                         lbs=Double.parseDouble(txtWeight.getText());
                         outputkgs = clLbs2Kgs.lbsTokgs(lbs);
                         txtConvWeight.setText(outkgs=bodymass.format(outputkgs));
                        }
                        // End of inch to cm conversion formula

    I would assume that if I use } else { this would work but it didnt, I have tried several variations but to no avail........any suggestions?

Similar Threads

  1. [SOLVED] If's
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 03:37 PM

Posting Permissions

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