1

I can't seem to figure out why it's not printing correctly:

public class test3 {
  public static void main(String[] args) {
    double[] gens = {100, 200.1, 9.3, 10};
    double d0 = 0;
    double d1 = 0;
    double d2 = 0;
    double d3 = 0;
    double d4 = 0;
    double d5 = 0;
    double d6 = 0;
    double d7 = 0;
    double d8 = 0;
    double d9 = 0;
    for (int i = 0; i < gens.length; i++) {
      double percs = gens[i];
      while (percs < -9 || 9 < percs) percs /= 10;
      percs = Math.abs(percs);
      if (percs == 0) {
        d0 += 1;
      }
      else if (percs == 1) {
        d1 += 1;
      }
      else if (percs == 2) {
        d2 += 1;
      }
      else if (percs == 3) {
        d3 += 1;
      }
      else if (percs == 4) {
        d4 += 1;
      }
      else if (percs == 5) {
        d5 += 1;
      }
      else if (percs == 6) {
        d6 += 1;
      }
      else if (percs == 7) {
        d7 += 1;
      }
      else if (percs == 8) {
        d8 += 1;
      }
      else if (percs == 9) {
        d9 += 1;
      }
    }
    double[] pack = {
       d0 /= gens.length,
       d1 /= gens.length,
       d2 /= gens.length,
       d3 /= gens.length,
       d4 /= gens.length,
       d5 /= gens.length,
       d6 /= gens.length,
       d7 /= gens.length,
       d8 /= gens.length,
       d9 /= gens.length
    };
    System.out.println(pack[0]);
    System.out.println(pack[1]);
    System.out.println(pack[2]);
    System.out.println(pack[3]);
    System.out.println(pack[4]);
    System.out.println(pack[5]);
    System.out.println(pack[6]);
    System.out.println(pack[7]);
    System.out.println(pack[8]);
    System.out.println(pack[9]);
  }
}

I know it's pretty repetitive, but that's just how I'm dealing with it for now.

Anyway, essentially, it should take those four numbers in the gens array, cut them off to the first digit of each number (i.e. 1, 2, 9 & 1 respectively), then count how many times each digit from 0-9 shows up, so '1' should give 0.5 (50%), 2 should give .25 (25%) and 9 should give .25 (25%) where every other digit from 0-9 should give 0. I get 0.5 for the digit '1', but 0 for every other one, and I haven't been able to find my mistake. The results I get vary depending on which numbers I input (hardcode), but they're pretty much always wrong.

Any ideas?

Thank you!

2 Answers 2

5

Change

double percs = gens[i];

to

int percs = (int)gens[i];

If you leave it as a double, your while-loop

while (percs < -9 || 9 < percs) percs /= 10;

will not properly cut off all the digits except the first because it will perform normal division not integer division. You can see this if you print percs after the while-loop.


I also want to point out that this can be done much more easily. For example:

double[] gens = {100, 200.1, 9.3, 10};

int[] count = new int[10];

for (double d : gens)
    count[("" + Math.abs(d)).charAt(0) - '0']++;

for (int n : count)
    System.out.println((double)n / gens.length);

will do the same thing.

2
  • ooh, I always forget when and when not to cast, thank you A.R.S and @Ina Also, I'm sure the method you suggested works great too, but I haven't learned a decent amount of that stuff yet, so I should stick to what I know for now.
    – Nate
    Oct 24, 2012 at 21:34
  • @Nate Sure, just wanted to point out that there are other alternatives :-) Anyway, glad I could help - don't forget to accept an answer!
    – arshajii
    Oct 24, 2012 at 21:36
1

If you print out percs at each stage you'll notice they are:

1.0
2.001
0.93
1.0

So your == will not work. This is because you are dividing doubles rather than integers. You can instead use:

int percs = (int) gens[i];

Which will give you the integer values you desire.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.