回答編集履歴

1

ネストが深くなりすぎるのが嫌なので処理順変更

2016/11/11 05:02

投稿

swordone
swordone

スコア20649

test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  並び順はComparableを付けてcompateToで判定させます。
6
6
 
7
+ (以下コードは動作未検証)
8
+
7
9
  ###Readerクラス
8
10
 
9
11
  ```java
@@ -42,67 +44,63 @@
42
44
 
43
45
 
44
46
 
45
- if (file.isFile()) {
47
+ if (!file.isFile()) {
48
+
46
-
49
+ System.out.println("ファイル指定が間違っています");
50
+
51
+ System.exit(-1);
52
+
53
+ }
54
+
47
- try (BufferedReader br = Files.newBufferedReader(Paths.get(args[0]), Charset.forName("MS932"))) {
55
+ try (BufferedReader br = Files.newBufferedReader(Paths.get(args[0]), Charset.forName("MS932"))) {
48
-
56
+
49
- // ファイルオープン
57
+ // ファイルオープン
50
-
58
+
51
- String line;
59
+ String line;
52
-
60
+
53
- final boolean acsend = br.readLine().equals("0");
61
+ final boolean acsend = br.readLine().equals("0");
54
-
62
+
55
- while ((line = br.readLine()) != null) {
63
+ while ((line = br.readLine()) != null) {
56
-
64
+
57
- try {
65
+ try {
58
-
59
- // ごちゃごちゃと判定を書く必要がなく、コードがスッキリする
66
+
60
-
61
- Mysort M = new Mysort(line);
67
+ Mysort M = new Mysort(line);
62
-
68
+
63
- tmpList.add(M);
69
+ tmpList.add(M);
64
-
70
+
65
- } catch (IllegalArgumentException e) {
71
+ } catch (IllegalArgumentException e) {
66
-
72
+
67
- e.printStackTrace();
73
+ e.printStackTrace();
68
-
69
- }
70
-
71
-
72
74
 
73
75
  }
74
76
 
75
- // 昇順、降順もこれで一発
77
+
76
-
77
- if (acsend) {
78
-
79
- Collections.sort(tmpList);
80
-
81
- } else {
82
-
83
- Collections.sort(tmpList, Collections.reverseOrder());
84
-
85
- }
86
-
87
-
88
-
89
- for (int i = 0; i < tmpList.size(); i++) {
90
-
91
- System.out.println(tmpList.get(i));
92
-
93
- }
94
-
95
- } catch (IOException e) {
96
-
97
- e.printStackTrace();
98
78
 
99
79
  }
100
80
 
81
+
82
+
83
+ if (acsend) {
84
+
85
+ Collections.sort(tmpList);
86
+
101
- } else {
87
+ } else {
88
+
102
-
89
+ Collections.sort(tmpList, Collections.reverseOrder());
90
+
91
+ }
92
+
93
+
94
+
95
+ for (int i = 0; i < tmpList.size(); i++) {
96
+
103
- System.out.println("ファイル指定が間違っています");
97
+ System.out.println(tmpList.get(i));
98
+
104
-
99
+ }
100
+
101
+ } catch (IOException e) {
102
+
105
- System.exit(-1);
103
+ e.printStackTrace();
106
104
 
107
105
  }
108
106