No older revisions available
No older revisions available
소스 코드 ¶
~java
import java.io.*;
import java.util.*;
public class Alice {
private BufferedReader br;
private Hashtable<String, Integer> map;
public Alice() {
try {
br = new BufferedReader(new FileReader("alice.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
map = new Hashtable<String, Integer>();
}
public String readLine() {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
public void splitWord() {
String line = null;
while(true) {
line = readLine();
if(line == null)
break;
Scanner sc = new Scanner(line);
while(sc.hasNext()) {
String word = sc.next();
word = word.replaceAll("[\p{Punct}]", "");
if (map.containsKey(word)) {
int n = map.get(word);
map.put(word, ++n);
}
else {
map.put(word, 1);
}
}
}
}
public static void main(String[] args) {
Alice alice = new Alice();
alice.splitWord();
ArrayList<Data> list = alice.sort();
alice.print(list);
}
private void print(ArrayList<Data> list) {
int size = list.size();
for(int i = 0; i < size; i++) {
String temp = list.get(i).getName();
int count = 0;
for(int j = 0; j < temp.length(); ++j) {
count = count + temp.toCharArray()[j];
}
System.out.printf("%-15s\t%d\t%d\n", temp, list.get(i).getCount(), count);
}
}
private ArrayList<Data> sort() {
Set set = map.keySet();
Iterator it = set.iterator();
ArrayList<Data> list = new ArrayList<Data>();
while(it.hasNext()) {
String key2 = (String)it.next();
int count = map.get(key2);
list.add(new Data(key2, count));
}
Collections.sort(list, new DataComparator());
return list;
}
@Override
protected void finalize() throws Throwable {
br.close();
}
}
class Data {
private String name;
private int count;
public Data(String name, int count) {
this.name = name;
this.count = count;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
}
class DataComparator implements Comparator<Data> {
public int compare(Data data1, Data data2) {
return data1.getName().compareTo(data2.getName());
}
}