0

I'm a beginner in java and I made a program to count the number of times an int d occurs in a given integer n i.e n = 988, d = 8 returns 2. It works with most cases (negative, positive, etc.) but my code says n = 0, d = 1 contains 1, which is wrong. How do I add a place to my 0 without making the integer 10 (which I erroneously do in my first if statement).

public class countingints{

    public static int count(int n, int d) {
        n = Math.abs(n); 
        if(n == 0) { 
            n = 10;
        }
        int result = 0;
        while (n > 0) {
            int place = n % 10; 
            if (place == d) {
                result++;
            }
            n /= 10;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(count(0, 1)); //SHOULD return 0
        System.out.println(count(0, 5)); //returns 0
    }
}
  • 1
    Please read: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) --- Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Jun 27 '21 at 19:05
  • 4
    `if(n == 0) { n = 10; }` What do you think this line is doing? – Louis Wasserman Jun 27 '21 at 19:05
  • 1
    Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Turing85 Jun 27 '21 at 19:06

1 Answers1

2
if(n == 0) { 
  n = 10;
}
```

Computer just follows instructions. If n is 0, n is set to 10, and 10 contains a single 1 digit.

If perhaps your intent with this if (n == 0) n = 10 line is to ensure that e.g. count(0, 0) returns 1, then just code that in: if (n == 0 && d == 0) return 1; - that's a weird case because mathematically speaking the question 'how many times does the digit 0 show up' is tricky. You can write 15 as 0015 as well, and in many ways, 0 is just a way of writing it, the number really can be considered just a completely blank string with no digits at all.

Point is, it's logical, more or less, that your algorithm does 'weird things' when you ask it for how many zeroes are in a number, in particular when you ask it how many zeroes are in the number 0. "Weird cases" usually mean they need hardcoding, so, do that.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Maybe just write `do { ... } while (n > 0)` to handle the `n=0` case? – Louis Wasserman Jun 27 '21 at 19:34
  • That works too. In the end, the algorithm's exit condition (stop when n is no longer above zero) is arbitrary. Mathematically speaking, '15' has infinite zeroes (all hanging on the left of it). The fact that `0` gets a count of 'one zero' and `15` doesn't stands separate from the algorithm. Doesn't really matter how you hack around it. There's a certain elegance to your solution. – rzwitserloot Jun 27 '21 at 19:36