Created
May 9, 2024 09:06
-
-
Save Exom9434/75f6322a4c22778e43c8d7a83338b456 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///노재경 | |
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); | |
List<String> lines = Files.readAllLines(path); | |
for (int i =0; i<lines.size(); i++){ | |
if(i> 0 ){ | |
sb.append("\n"); | |
} | |
sb.append(lines.get(i)); | |
} | |
} 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); | |
HashMap<Character, Integer> counts = new HashMap<Character, Integer>( ); | |
for(char c = 'A'; c <= 'Z'; c++) { | |
counts.put(c, 0); | |
} | |
for(int i = 0; i <text.length(); i++){ | |
Character c = Character.toUpperCase(text.charAt(i)); | |
if(counts.containsKey((c))){ | |
counts.put(c, counts.get(c) + 1); | |
} | |
} | |
for(char c = 'A'; c <= 'Z'; c++){ | |
int number = counts.get(c); | |
float percent = (float) counts.get(c) / text.length() * 100; | |
String message = "%c= %d개, %.2f%%".formatted(c, number, percent); | |
System.out.println(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment