Calculating men's risk of getting coronary heart disease I'd like anyone here to help me clean up my code. It's about calculating men's risk of getting coronary heart disease. Here's the link to my method.

private void calculateForMen(){

 // TODO
 // For Men
 // 20–34 years: Minus 9 points. 
 // 35–39 years: Minus 4 points. 
 // 40–44 years: 0 points. 
 // 45–49 years: 3 points.
 // 50–54 years: 6 points. 
 // 55–59 years: 8 points. 
 // 60–64 years: 10 points. 
 // 65–69 years: 11 points. 
 // 70–74 years: 12 points. 
 // 75–79 years: 13 points.

        // TODO Men Age 20-34
        if( strAge.equals("20 to 34") ){ // Minus 7 points

            // First point
            points = points - 9;

            // Second point
            if( strTotalCholesterol.equals("160 to 199") ){// 4 points
                points = points + 4;
            }
            else if( strTotalCholesterol.equals("200 to 239") ){ // 8 points
                points = points + 7;
            }
            else if( strTotalCholesterol.equals("240 to 279") ){ // 11 points
                points += 9;
            }
            else if( strTotalCholesterol.equals("280 or greater") ){ // 13 points
                points += 11;
            }

            // Third point
            if( strHDL.equals("60 or greater") ) // Minus 1 point
            {
                points -= 1;
            }
            else if( strHDL.equals("40 to 49") ){ // 1 point
                points += 1;
            }
            else if( strHDL.equals("Less than 40") ){ // 2 points
                points += 2;
            }

            // Fourth point
            if( strSBP.equals("130 to 139") ){ // 2 points
                points += 1;
            }
            else if( strSBP.equals("140 to 159") ){ // 3 points
                points += 1;
            }
            else if( strSBP.equals("160 or greater") ){ // 4 points
                points += 2;
            }
Each of your if blocks are very similar. You basically the same thing in each if block. This a sign that you've got the conditionally at the wrong level. Instead of writing code that does:


If age category 1

Add points for age
Add points for cholestoral
etc

If age category 2

Add points for age
Add points for cholestoral
etc

etc


Write it like this:


Add points for age

Handle age category 1
Handle age category 2
etc

Add points for cholestoral

Handle age category 1
Handle age category 2
etc

etc


So adding the age points should look something like:

if( strAge.equals("20 to 34") ){ // Minus 7 points
    points = points - 9;
}
else if( strAge.equals("35 to 39") ){
    points = points - 4;
}
else if( strAge.equals("35 to 39") ){
    points = points - 4;
}
else if( strAge.equals("40 to 44") ){
    points += 0;
} 
else if( strAge.equals("45 to 49") ){
    points += 3;
}
else if( strAge.equals("50 to 54") ){
    points += 6;
}
else if( strAge.equals("55 to 59") ){
    points += 8;
}
else if( strAge.equals("60 to 64") ){
    points += 10;
}
else if( strAge.equals("65 to 69") ){
    points += 11;
}
else if( strAge.equals("70 to 74") ){
    points += 12;
}
else if( strAge.equals("75 to 79") ){
    points += 13;
}


But doesn't that repeat the .equals way too much? Yes. But the repetition here is much simpler and easier to deal with then what you had before. So we prefer this. Once we have this we can simplify it.

The next key is to use data not code. That is, make lists of things and look them up rather then putting them in code. 

static String [] ageCategories = {"20 to 34", "35 to 39", "40 to 44", "45 to 49", "50 to 54", "55 to 59", "60 to 64", "64 to 69", "70 to 74", "75 to 79"}
static int agePoints = {-9, -4, -4, 0, 3, 6, 8, 10, 11, 12, 13};

points += agePoints[Arrays.asList(ageCategories).indexOf(strAge)];


This looks up the age level and the points by looking in the arrays. No ifs required. You should basically not need to use any if statements whatsoever in this particular code. You should rewrite it to fetch everything out of arrays like this.

As the number of age ranges increases, it may be easier to work with a map because each point adjustment is matched up to its corresponding age range.

static final Map&lt;String, Integer&gt; pointsByAgeRange = new HashMap&lt;&gt;() {{
    put("20 to 34", -9);
    put("35 to 39", -4);
    ...
    put("75 to 79", 13);
}};

points += pointsByAgeRange.get(strAge);  // assumes range is in map