- public class Test {
- public static void main(String[] args) {
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
-
- StringSort ss = new StringSort();
- TreeSet<String> strings = new TreeSet<String>(ss);
- String line = null;
- try {
- while((line = input.readLine())!= null){
- if("end".equals(line)){
- break;
- }
- strings.add(line);
- }
- Iterator<String> it = strings.iterator();
- while(it.hasNext()){
- String str = it.next();
- out.write(str);
- out.newLine();
- out.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(input != null){
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(out != null){
- try {
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- class StringSort implements Comparator<String>{
- @Override
- public int compare(String s1, String s2) {
- return s2.compareTo(s1);
- }
复制代码 怎么才能让这段代码正确的将录入的字符串按照字典倒序打印出来? |
|