0

I would like to ask you a question, I want to count how many times a specified number is in a given numer (long) from the user and print it as an int.

Could you please help me with this code?

Apologize for my english.

Example:

< number specified = 4; number given = 34434544; result = 5. >

Attix
  • 13
  • 4
  • Convert to String, then read this http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – Mark B Apr 18 '16 at 18:36
  • No need to convert to a String: just test the value with `% 10`, see if it is equal to the 4; then divide value by 10. Repeat until value is zero. – Andy Turner Apr 18 '16 at 18:38
  • I'll try it out, thank you all! – Attix Apr 18 '16 at 18:40
  • Just a question, how can I somehow test it with a long n and then return it as an int without loosing data? – Attix Apr 18 '16 at 18:42
  • Simple: there are a lot fewer than `Integer.MAX_VALUE` digits in both `Long.MIN_VALUE` and `Long.MIN_VALUE`. – Andy Turner Apr 18 '16 at 18:46

3 Answers3

1

Check whether the least-significant digit is equal to the digit you are searching for; then divide by ten and check again; keep going until you reach zero.

int cnt = 0;
while (value != 0) {
  if (value % 10 == digit) ++cnt;
  value /= 10;
}

Where you are trying to count the occurrences of digit in the big number value.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Make a Scanner to get input from System.in. Then, iterate through the String returned by turning it into a char[]. Then analyse each char and count original characters. Probably use a Map<Character, Integer> for this. For each element in the Map, iterate by one if it is in the Map. Query the Map for your desired character and print the result when finished.

public static void main(String[] args) {

    CharacterFinder cf = new CharacterFinder();

    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();

    Map<Character, Integer> resultsMap = cf.countChar(input);
    System.out.println(resultsMap.get('g'));
}

// Note that 'null' means that it does not appear and if it is null, you ought print 0 instead.
// Also note that this method is case sensitive.
private Map<Character, Integer> countChar(String input) {

    Map<Character, Integer> resultsMap = new HashMap<Character, Integer>();
    for (int i = 0; i < input.length(); i++) {
        Character element = input.charAt(i);
        if (resultsMap.containsKey(element)) {
            Integer cCount = resultsMap.get(element);
            resultsMap.put(element, cCount + 1);
        } else {
            resultsMap.put(element, 1);
        }
    }

    return resultsMap;
}

Well, unless you already know the char you want. In that case, analyse for that exact char.

public static void main(String[] args) {
    CharacterFinder cf = new CharacterFinder();

    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();

    // Call counting method and print
    System.out.println(cf.countChar(input, '5'));
}

// Counting method
private int countChar(String input, char c) {
    int x = 0;
    for (int i = 0; i < input.length(); i++) {
        if (input.charAt(i) == c) {
            x++;
        }
    }
    return x;
}
ifly6
  • 5,003
  • 2
  • 24
  • 47
0

If you're using Java 8:

long count = String.valueOf(givenNumber).chars()
        .filter(c -> c == (specifiedNumber + '0'))
        .count();
ericbn
  • 10,163
  • 3
  • 47
  • 55