Hi to everyone.
I'm a beginner at Java and I'm studying a software develoopment course right now. I have a problem with my first assignment. In particular with the last exercise of the assignment that is about writing a program that shows a character distribution with a counter that shows how many times some character appears in a inputted string. I know I must set a Scanner, an int array and 2 'for' loops (one scanning the string inputted through Scanner and another to count for each Unicode chracter). My problem here is how can I count for each Unicode character and to add the count to the int array and ultimately output in a console each character with its count through 'System.out.println()'.

Thanks in advance.

this code might be useful to you,

import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class CharacterDistribution {
 
	public static void main(String[] args) {
		HashMap<Character, Integer> charDist = new HashMap<Character, Integer>();

		Scanner input =  new Scanner (System.in);
		input.useDelimiter("\n");
 		System.out.print("Enter Text :");
		String strText=input.next();
		
		char[] textChars = strText.toCharArray();
		
		for(int i=0; i<textChars.length-1; i++){
			Character currChar = new Character(textChars[i]);
			Integer cnt = charDist.get(currChar);
			if(cnt == null){
				charDist.put(currChar, new Integer(1));
			}
			else{
				cnt++;
				charDist.put(currChar, cnt);
			}
		}
 
		System.out.println("The character distribution is :");
		List<Character> keys = new ArrayList<Character>(charDist.keySet());
		Collections.sort(keys);
		Iterator itr = keys.iterator();
		while(itr.hasNext()) {
			Character currChar = (Character)itr.next();
			System.out.println(currChar+" : "+charDist.get(currChar));
		}
	}
 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.