Skip to content

Instantly share code, notes, and snippets.

@Exom9434
Created January 3, 2025 13:35
Show Gist options
  • Save Exom9434/ea59fbf1dfaa9da163b3393917564f87 to your computer and use it in GitHub Desktop.
Save Exom9434/ea59fbf1dfaa9da163b3393917564f87 to your computer and use it in GitHub Desktop.
//노재경
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
public class Main {
public static class FileUtils {
public String getLoadText(String filePath) {
StringBuilder sb = new StringBuilder();
try {
Path path = Paths.get(filePath);
Files.lines(path).forEach(line -> sb.append(line).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
public static void main(String[] args) {
FileUtils fileUtil = new FileUtils();
String filePath = "G:\\내 드라이브\\제로베이스스쿨_백엔드스쿨\\part-01.-java-기초---마종현-강사님\\part-01.-java-기초---마종현-강사님\\Part 01. Java 기초 _소스코드\\Homework\\Homework2\\src\\President.txt";
String text = fileUtil.getLoadText(filePath);
if (text.isEmpty()) {
System.out.println("The file is empty or could not be read.");
return;
}
HashMap<Character, Integer> counts = new HashMap<>();
for (int i = 0; i < text.length(); i++) {
char c = Character.toUpperCase(text.charAt(i));
if (Character.isLetter(c)) {
counts.compute(c, (key, value) -> (value == null) ? 1 : value + 1);
}
}
int totalLetters = counts.values().stream().mapToInt(Integer::intValue).sum();
for (char c = 'A'; c <= 'Z'; c++) {
int number = counts.getOrDefault(c, 0);
float percent = (float) number / totalLetters * 100;
String message = "%c= %d개, %.2f%%".formatted(c, number, percent);
System.out.println(message);
}
}
}
Key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment