Last active
September 15, 2022 01:31
-
-
Save kingseungil/7c390975eba2d1eafd695d2817255a7a 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
/* | |
김승일 | |
깜짝과제2 | |
*/ | |
package s_project; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.List; | |
public 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(); | |
} | |
} | |
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
/* | |
김승일 | |
깜짝과제1 | |
*/ | |
import java.util.Locale; | |
public class s_project2 extends FileUtils { | |
public static void main(String[] args) { | |
String filePath = "C:\\Users\\HOME\\IdeaProjects\\miniProject\\src\\s_project\\textSample.txt"; | |
FileUtils fileUtils = new FileUtils(); | |
String fileText = fileUtils.getLoadText(filePath); | |
fileText = fileText.toLowerCase(); | |
int[] alphaCnt = { | |
0,0,0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
}; | |
int totalCount = 0; | |
for (int i = 0; i < fileText.length(); i++) { | |
char c1 = fileText.charAt(i); | |
if (c1 == 'a'){alphaCnt[0]++;} | |
else if (c1 == 'b'){alphaCnt[1]++;} | |
else if (c1 == 'c'){alphaCnt[2]++;} | |
else if (c1 == 'd'){alphaCnt[3]++;} | |
else if (c1 == 'e'){alphaCnt[4]++;} | |
else if (c1 == 'f'){alphaCnt[5]++;} | |
else if (c1 == 'g'){alphaCnt[6]++;} | |
else if (c1 == 'h'){alphaCnt[7]++;} | |
else if (c1 == 'i'){alphaCnt[8]++;} | |
else if (c1 == 'j'){alphaCnt[9]++;} | |
else if (c1 == 'k'){alphaCnt[10]++;} | |
else if (c1 == 'l'){alphaCnt[11]++;} | |
else if (c1 == 'm'){alphaCnt[12]++;} | |
else if (c1 == 'n'){alphaCnt[13]++;} | |
else if (c1 == 'o'){alphaCnt[14]++;} | |
else if (c1 == 'p'){alphaCnt[15]++;} | |
else if (c1 == 'q'){alphaCnt[16]++;} | |
else if (c1 == 'r'){alphaCnt[17]++;} | |
else if (c1 == 's'){alphaCnt[18]++;} | |
else if (c1 == 't'){alphaCnt[19]++;} | |
else if (c1 == 'u'){alphaCnt[20]++;} | |
else if (c1 == 'v'){alphaCnt[21]++;} | |
else if (c1 == 'w'){alphaCnt[22]++;} | |
else if (c1 == 'x'){alphaCnt[23]++;} | |
else if (c1 == 'y'){alphaCnt[24]++;} | |
else if (c1 == 'z'){alphaCnt[25]++;} | |
} | |
for (int i = 0; i < alphaCnt.length; i++) { | |
totalCount += alphaCnt[i]; | |
} | |
for (int i = 0; i < alphaCnt.length; i++) { | |
double rate = (double)alphaCnt[i] / totalCount * 100; | |
String format = String.format("%c = %6d개, \t %.2f%%",i+65,alphaCnt[i],rate); | |
System.out.println(format); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment