StringCharacterFrequency is a small Java console program that:
- Reads a string from the user
- Counts how many times each non‑whitespace character appears
- Prints result in the order of the first appearance of each character
- Repeats until you choose to exit
Example
- Input:
hello world - Output:
h:1, e:1, l:3, o:2, w:1, r:1, d:1(the space is ignored)
- Ignores all Unicode whitespace (spaces, tabs, newlines, etc.)
- Counts letters (Latin, Cyrillic, …), digits, punctuation, and other non‑whitespace code points
- Keeps the order of first appearance
- Simple menu:
1= continue,2= exit (only exact"1"/"2"are accepted)
- Java 21 (Java 17+ also works)
- Maven 3.9+ (optional, for building/running tests)
From the project root:
# Compile main sources
mvn -q compile
# Run the program
java -cp target/classes StringCharacterFrequencyRun tests:
mvn testFrom the project root:
# Compile into ./out directory
javac -d out src/main/java/StringCharacterFrequency.java
# Run
java -cp out StringCharacterFrequencyEnter a string: Hello хелоу
Character counts: H:1, e:1, l:2, o:1, х:1, е:1, л:1, о:1, у:1
Do you want to continue?
1) Yes
2) No
Choose an option (1 or 2) and press Enter: 2
Exiting program. Goodbye!
Notes
- Whitespace is not counted.
- Ordering is based on first appearance, not alphabetical order.
- The program is Unicode‑aware (iterates by code points), so emojis and characters outside the BMP would be handled correctly if entered.
StringCharacterFrequency/
├─ pom.xml
├─ README.md
└─ src/
├─ main/java/StringCharacterFrequency.java
└─ test/java/StringCharacterFrequencyTest.java
- If
javacannot find the class, ensure you run from the project root and that the-cp(classpath) points totarget/classes(Maven) or the directory you compiled to (e.g.,out). - On older Java versions, make sure your
JAVA_HOMEpoints to a JDK (not a JRE).
This project is provided as‑is for educational purposes. Use freely.