0

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?

asked Sep 7, 2013 at 3:19
3
  • 1
    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; Commented Sep 7, 2013 at 3:52
  • Aaaand it works. Thanks a million, Douglas!! I'm pretty crafty with Java but I always seem to get hung up on these simple errors... One of these days I'll get it right. Thanks again!! Commented Sep 7, 2013 at 4:05
  • 1
    @DouglasJones you should write this as an answer so that it can be accepted :) Commented Sep 7, 2013 at 4:22

1 Answer 1

1

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; 
answered Sep 7, 2013 at 13:19
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.