-3
public static void main(String[] args) {
    int count = 0;
    char[] array = {'а', 'g', 'r', 'e', 'r', 's', 'a', 'х', 'ј', 'a'};
    //char[] array = {'p', 'a', 'а', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'а'};
    //char[] array = {'a', 'b', 'b', 'c', 'k', 'a', 'a'};

    for (int i = 0; i < array.length; i++) {
        if (array[i] == 'a') {
            count++;
        }
        //System.out.println(count);
    }
    System.out.println("Letter 'a' " + count + " times.");
}

I have some 'specific' problem here. If I check first array, result is 2 instead 3. If I check second array, result is 8 instead 10. When I check third array, everything is OK.

You can uncomment the System.out.println(count); line and see strange way how it wrong count.

I want to know what is problem.

Thanks in advance. Regards.

Bucket
  • 7,415
  • 9
  • 35
  • 45

1 Answers1

10

The first 'а' is not an a.

http://unicode.scarfboy.com/?s=%D0%B0

Instead, it is CYRILLIC SMALL LETTER A (encoded as U+0430), which is not the same as the a (encoded at U+0061 with name LATIN SMALL LETTER A) that is normally used in the Latin alphabet. Java is not wrong, there are in fact two elements which are Latin a's and encoded at the Latin a's code point.

Going through the second array, this problem persists in the inputs, at least at the second 'a' in the second array.

This appears because character evaluation like that you have above is done by comparing the code points (which also is why char types can be treated as integers). Under the hood, the computer is basically asking itself whether 61 is 430, which is obviously untrue.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • 1
    seems like OP was typing using the wrong language settings – Kaelan Mikowicz Sep 06 '18 at 18:06
  • 1
    @ifly6 how do u detect it? – The Scientific Method Sep 06 '18 at 18:11
  • you can cast the characters to an `ìnt` and print them or view the source with a hex editor... – rdmueller Sep 06 '18 at 18:12
  • 1
    I detected it by copy-pasting the `a`'s into the Unicode search doohicky linked above, which quickly yielded the answer after one or two tries. You could also just cast them to `int` and see what's under the hood as described above. – ifly6 Sep 06 '18 at 18:13
  • @ifly6 Thank you. Can you or anybody else give me the simlest way to get this code work? In fact, I need to count every letter in array that appears more then 1 time and show results. This was my start point, but I get that problem on start... – Arcibald Rajs Sep 06 '18 at 18:36
  • I would map the whole array into a `HashMap` which is set up to work as a `HashBag`. – ifly6 Sep 06 '18 at 18:41
  • @ifly6 I need simplest way, without using of collections, etc. We don't learn collections on college, that is reason why I need solution with basic level of java. Hope you understand me. – Arcibald Rajs Sep 06 '18 at 18:45
  • This is a one-liner which sets up the map and counts the objects: `IntStream.range(0, array.length).mapToObj(i -> array[i]).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))` – ifly6 Sep 06 '18 at 18:46
  • @ifly6 That is too advanced syntax for our level. :) I need to find solution with loops, basic comparing, etc... – Arcibald Rajs Sep 06 '18 at 18:49