0

I'm trying to make a program that asks a user for two numbers. I want to determine how many times a certain value appear in the sequence of numbers. For instance, if user entered 10 and 20 the number "1" would appear 9 times. What I am wondering is what condition would I have to set to see how many times the number "1" would appear.

This is what I got so far...

System.out.println("Enter a number: ");
    int no1 = scan.nextInt();
    System.out.println("Enter a number: ");
    int no2 = scan.nextInt();

    int i;
    int j = 0;

    for(i = no1; i <= no2; i++){
        if(){                      // Some condition here
            j++;
        }
    }
    System.out.println(j);
}

Any other tips and tricks on how to make my code efficient would also be greatly appreciated.

  • `For instance, if user entered 10 and 20 the number "1" would appear 9 times.` - It's unclear what you mean by this. The number 1 doesn't appear at all between 10 and 20, and the numeral 1 appears 10 or 11 times as part of a number depending on whether your boundaries are inclusive or exclusive. – azurefrog Oct 18 '16 at 16:48
  • "For instance, if user entered 10 and 20 the number "1" would appear 9 times." - Why is that? It only appears once. – Turing85 Oct 18 '16 at 16:49
  • including 10 it appears 11 times isnt it... 10-1, 11-2 and so on.. let me give you a hint.. you need to use modulo operator and compare every digits in a number.. what if the user enters the number between 110 to 120 – Shreyas Sarvothama Oct 18 '16 at 16:59
  • My mistake. Shreyas is correct the boundaries are inclusive and in that case the number "1" would appear 11 times. I was messing around with the modulus operator but I never knew what to set the remainder equal to--and that fact that I would have to compare every digit (not just the one's place) makes it more confusing. – Construct0r Oct 18 '16 at 17:04
  • Possible duplicate of [How to get the separate digits of an int number?](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number) –  Oct 23 '16 at 18:34

2 Answers2

1
for (int i = no1; i <= no2; i++) {
  if(String.valueOf(i).contains("1"))
      int occurances = StringUtils.countOccurrencesOf(String.valueOf(i), "1");
      j+=occurances
}
0

Assuming that you want to count the occurrences of a digit within a list of numbers you could e.g. use modulo:

int[] occurrences = new int[10];
for (int i = n1; i <= n2; i++) {
    int currentNumber = Math.abs(i);
    while (currentNumber > 0) {
        occurrences[currentNumber % 10]++;
        currentNumber /= 10;
    }
}

or parse the string representation of the number

int[] occurrences = new int[10];
for (int i = n1; i <= n2; i++) {
    String cur = String.valueOf(Math.abs(i));
    for (int j = 0; j < cur.length(); j++) {
            char currentChar = cur.charAt(j);
            if (currentChar != '-')
                occurrences[Character.getNumericValue(currentChar)]++;
    }
}

occurrences will contain the counts for digits from 0 to 9;

Edit: Added handling for negative integers.

dudel
  • 684
  • 5
  • 15
  • You used an array so the condition in the while loop is valid. What if the user entered a negative number for the lower limit, how would the while loop condition change? – Construct0r Oct 18 '16 at 17:13
  • Also, occurrences[currentNumber % 10]++; currentNumber /= 10; – Construct0r Oct 18 '16 at 17:14
  • I just edited the code to account for negative integers by using absolute numbers with Math.abs. In regard to the lines the `% 10` gets the last digit of the number and increments the count for that digit in the array. `currentNumber /= 10` divides currentNumber by 10 and assigns the result back to the variable. – dudel Oct 18 '16 at 17:32