Implement a Java program to count the occurrence of each character in a string.

Here’s a Java program to count the occurrence of each character in a string:

import java.util.HashMap;
import java.util.Scanner;

public class CharCount {
    public static void main(String[] args) {
        String str;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        str = scanner.nextLine();
        HashMap charCount = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (charCount.containsKey(c)) {
                charCount.put(c, charCount.get(c) + 1);
            } else {
                charCount.put(c, 1);
            }
        }
        System.out.println("Character count: ");
        for (char c : charCount.keySet()) {
            System.out.println(c + " - " + charCount.get(c));
        }
    }
}

In this program, we first take user input for a string. We then create a HashMap called `charCount` to store the count of each character in the string.

We then use a for loop to iterate over each character in the string. For each character, we check if it is already in the `charCount` HashMap. If it is, we increment its count by 1. If it is not, we add it to the HashMap with a count of 1.

At the end of the program, we print out the count of each character in the string using another for loop to iterate over the keys of the `charCount` HashMap.

Note that this program is case-sensitive, so uppercase and lowercase letters are counted separately. If you want to make it case-insensitive, you can convert all characters to lowercase or uppercase before counting them.