Nicolas Mustaro
Nicolas Mustaro

Reputation: 681

Strange, code-specific issue, probably logical error?

Ok, so I have a program that takes three digit numbers and calculates how many times each digit, 0-9, occurs through all entered numbers and gives a percentage of each. For some reason, when I enter a number like "111" or "222" it works correctly and sets that numbers percentage to 100%. If I add another number, like "123", it returns all the percentages as zeroes. Here's the code:

Declarations:

private List<PickThreeNumbers> numberList = new ArrayList();
private static int[] timesOccured = {0,0,0,0,0,0,0,0,0,0};
private static double[] percentages = {0,0,0,0,0,0,0,0,0,0};
private static int totalNumbers;
private DecimalFormat dm = new DecimalFormat("0.00");
private static String percentage, timesOccuredString;

This calculates how many times each individual digit occurs in the list of numbers:

    private void calculateTimesOccured(){
    resetAllCalculations();
    for(int i = 0; i < numberList.size();i++){
        for(int r = 1; r <=3; r++){
            for(int b = 0; b <=9; b++){
                if (numberList.get(i).getSingleDigit(r) == b){
                    timesOccured[b]+=1;
                }
            }
        }
    }
}

Then, this calculates the percentages:

private void calculatePercentages(){
    for(int w = 0; w < timesOccured.length; w++){
        totalNumbers+=timesOccured[w];
    }

    for(int e = 0; e < percentages.length; e++){
        percentages[e] = timesOccured[e] / totalNumbers;
        percentages[e]*=100;
    }
}

Then, this returns the percentages to the main activity:

public String getPercentage(int digit){
    for(int t = 0; t<percentages.length;t++){
        if(digit == t){
            percentage= dm.format(percentages[t]);
        }
    }
    return percentage;
}

Finally, this is the method in the main activity class that generates the table dynamically:

percentages.setStretchAllColumns(true);
    percentages.removeAllViews();
    for(int i = 0; i <=9; i++){
        TableRow tr = new TableRow(this);
        TextView digit = new TextView(this);
        TextView timesOccured = new TextView(this);
        TextView percent = new TextView(this);

        digit.setText(String.valueOf(i)+":");
        timesOccured.setText(numbers.getTimesOccuredString(i));
        percent.setText(numbers.getPercentage(i));

        digit.setTextColor(Color.WHITE);
        timesOccured.setTextColor(Color.WHITE);
        percent.setTextColor(Color.WHITE);

        tr.addView(digit);
        tr.addView(timesOccured);
        tr.addView(percent);

        if(i%2 != 0){
            tr.setBackgroundColor(Color.parseColor("#333333"));
        }else{
            tr.setBackgroundColor(Color.parseColor("#444444"));
        }

        percentages.addView(tr);
    }

This is the declarations in the main activity class:

TableLayout numbersEntered, percentages, recommendedNumbers;
NumberHandler numbers;

with this in the OnCreate method:

 numbersEntered = (TableLayout)findViewById(R.id.enteredNumbersTable);
    percentages = (TableLayout)findViewById(R.id.percentagesTable);
    recommendedNumbers=(TableLayout)findViewById(R.id.recNumbersTable);
    numbers = new NumberHandler();

Nomatter what happens, if I enter more than one particular digit (i.e. if I enter, "222", "222", it works by setting "2" to "100%", but if I add any other digits it sets them all to zero.)

Any ideas?

Upvotes: 0

Views: 63

Answers (1)

Douglas Jones
Douglas Jones

Reputation: 2542

in calculatePercentages you need to convert the numbers being divided to doubles. You are doing integer math which is either going to come out as 0 or 1.

percentages[e] = (double)timesOccured[e] / (double)totalNumbers; 

Upvotes: 1

Related Questions