質問編集履歴

2

文法、エラーコード更新

2017/06/08 05:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -16,11 +16,13 @@
16
16
 
17
17
  ```
18
18
 
19
- Exception in thread "main" java.lang.NullPointerException
19
+ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
20
-
20
+
21
- at traning.Test.<init>(Test.java:33)
21
+ 到達不能コード
22
-
22
+
23
+
24
+
23
- at traning.Test.main(Test.java:21)
25
+ at traning.Test.main(Test.java:36)
24
26
 
25
27
 
26
28
 

1

文法の修正

2017/06/08 05:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -34,17 +34,11 @@
34
34
 
35
35
  ```Java
36
36
 
37
- package traning;
37
+ package traning
38
-
39
-
40
-
41
- import java.io.BufferedReader;
38
+
42
-
39
+
40
+
43
- import java.io.FileReader;
41
+ import java.nio.charset.Charset;
44
-
45
- import java.io.IOException;
46
-
47
- import java.net.URI;
48
42
 
49
43
  import java.nio.file.Files;
50
44
 
@@ -64,182 +58,190 @@
64
58
 
65
59
  public class Test{
66
60
 
67
-
68
-
61
+
62
+
69
- public static void main(String[] args) {
63
+ public static void main(String[] args) {
70
64
 
71
65
 
72
66
 
67
+ //自インスタンス生成
68
+
73
- Test tes = new Test();
69
+ Test test = new Test();
70
+
71
+
72
+
74
-
73
+ //引数の確認
75
-
76
-
77
-
78
74
 
79
75
  if (args.length != 1) {
80
76
 
77
+ System.out.println("引数を指定してください");
78
+
79
+ return;
80
+
81
+ }
82
+
83
+
84
+
85
+ //パスオブジェクトをgetメソッドで作成
86
+
87
+ Path path = Paths.get(args[0]);
88
+
89
+ if(!Files.exists(path));{
90
+
81
- System.out.println("ファイルが開けないか存在しません。");
91
+ System.out.println("ファイルが開けないか存在しません。");
92
+
93
+ return;
94
+
95
+ }
96
+
97
+
98
+
99
+ //ファイル読込
100
+
101
+ List<String> list;
102
+
103
+ try{
104
+
105
+ list = Files.readAllLines(path,Charset.forName("SJIS"));
106
+
107
+
108
+
109
+ }catch( IOException e ) {
110
+
111
+ e.printStackTrace();
112
+
113
+ System.out.println("ファイルパスが正しくありません");
82
114
 
83
115
  return;
84
116
 
85
- }
117
+ }
118
+
119
+
120
+
121
+ /*データ抽出呼び出し*/
122
+
123
+ List<String> ProductList = rac.ExtractProduct(list);
124
+
125
+ List<Integer> PriceList = rac.ExtractPrice(list);
126
+
127
+
128
+
129
+ /*商品名と価格画面表示*/
130
+
131
+ for(int count = 0; count<PriceList.size();count++){
132
+
133
+ System.out.print(ProductList.get(count) + "は");
134
+
135
+ System.out.println(PriceList.get(count)+ "円");
86
136
 
87
137
  }
88
138
 
89
-
90
-
91
- private URI[] args;
139
+
92
-
93
-
94
-
95
-
96
-
140
+
97
- Path Path = Paths.get(args[0]);{
141
+ int total = test.totalAmount(PriceList);
142
+
143
+
144
+
98
-
145
+ }
146
+
147
+
148
+
149
+ List<String> ExtractProduct(List<String> list){
150
+
151
+ List<String> ProductList = new ArrayList<String>();
152
+
153
+ //商品名抽出
154
+
155
+ for (int i = 0; i<list.size(); i++){
156
+
157
+ String regexProductName = "\\[(.+)\\]";
158
+
159
+ Pattern patternProduct = Pattern.compile(regexProductName);
160
+
161
+ Matcher matchProduct = patternProduct.matcher(list.get(i));
162
+
99
- if(!Files.exists(Path)){
163
+ if(matchProduct.find()){
100
-
164
+
101
- System.out.println("ファイルが開けないか存在しません。");
165
+ ProductList.add(matchProduct.group(0));
166
+
102
-
167
+ //抽出不可
168
+
103
-
169
+ }else{
170
+
171
+ ProductList.add(null);
172
+
173
+ }
104
174
 
105
175
  }
106
176
 
107
-
108
-
109
- try{
177
+ return ProductList;
178
+
179
+
180
+
110
-
181
+ }
182
+
183
+ List<Integer> ExtractPrice (List<String> list){
184
+
185
+ List<Integer> PriceList = new ArrayList<Integer>();
186
+
187
+ //価格抽出
188
+
189
+ for (int j = 0; j<list.size(); j++){
190
+
111
- FileReader fr = new FileReader("c:¥¥tmp¥¥receipt.txt");
191
+ String regexPriceName = "\\\\(\\d+)";
112
-
192
+
113
- BufferedReader br = new BufferedReader( fr );
193
+ Pattern pattrenPrice = Pattern.compile(regexPriceName);
194
+
114
-
195
+ Matcher matchPrice = pattrenPrice.matcher(list.get(j).toString());
115
-
116
-
117
-
118
-
196
+
119
- }catch( IOException e ) {
197
+ if(matchPrice.find()){
198
+
120
-
199
+ PriceList.add(Integer.parseInt(matchPrice.group(1)));
200
+
201
+ //抽出不可
202
+
203
+ }else{
204
+
121
- e.printStackTrace();
205
+ PriceList.add(0);
122
-
206
+
123
- System.out.println("読み込み不可。");
207
+ }
208
+
209
+
210
+
211
+
124
212
 
125
213
  }
126
214
 
127
-
215
+ return PriceList;
216
+
128
-
217
+ }
218
+
219
+
220
+
129
-
221
+ /*合計金額算出処理*/
222
+
223
+ int totalAmount(List<Integer> PriceList){
224
+
225
+ int total =0;
226
+
227
+ for(int k =0; k<PriceList.size(); k++){
228
+
229
+ total += PriceList.get(k);
230
+
231
+ }
232
+
233
+ System.out.println("合計金額は"+total+"円です");
234
+
235
+
236
+
237
+ return total;
238
+
239
+
240
+
241
+
130
242
 
131
243
  }
132
244
 
133
-
134
-
135
- List<String> ProductExtraction (List<String> list){
136
-
137
- List<String> ProductList = new ArrayList<String>();
138
-
139
- for (int i = 0; i<list.size(); i++){
140
-
141
- String regexProductName = "(\\[)(\\])";
142
-
143
- Pattern patternProduct = Pattern.compile(regexProductName);
144
-
145
- Matcher matchProduct =patternProduct.matcher(list.get(i));
146
-
147
-
148
-
149
- if(matchProduct.find()){
150
-
151
- ProductList.add(matchProduct.group(0));
152
-
153
- }else{
154
-
155
- ProductList.add(null);
156
-
157
- System.err.println("抽出不可");
158
-
159
- }
160
-
161
- }
162
-
163
- return ProductList;
164
-
165
-
166
-
167
- }
168
-
169
-
170
-
171
-
172
-
173
- List<Integer> PriceExtraction (List<Integer> list){
174
-
175
- List<Integer> PriceList = new ArrayList<Integer>();
176
-
177
- for (int j = 0; j<list.size(); j++){
178
-
179
- String regexPriceName = "\\d+";
180
-
181
-
182
-
183
- Pattern pattrenPrice = Pattern.compile(regexPriceName);
184
-
185
- Matcher matchPrice = pattrenPrice.matcher(list.get(j).toString());
186
-
187
- if(matchPrice.find()){
188
-
189
- PriceList.add(Integer.parseInt(matchPrice.group(1)));
190
-
191
- }else{
192
-
193
- PriceList.add(null);
194
-
195
- System.err.println("抽出不可");
196
-
197
- }
198
-
199
-
200
-
201
- }
202
-
203
-
204
-
205
- return PriceList;
206
-
207
-
208
-
209
- List<String> ProductList = tes.ProductExtraction(list);
210
-
211
- List<Integer> PriceList = tes.PriceExtraction(list);
212
-
213
-
214
-
215
-
216
-
217
- for(int count = 0; count<PriceList.size();count++){
218
-
219
- System.out.println(ProductList.get(count) + "は");
220
-
221
- System.out.println(PriceList.get(count)+ "円");
222
-
223
- }
224
-
225
-
226
-
227
- int total =0;
228
-
229
- for(int k =0; k<PriceList.size(); k++){
230
-
231
- total +=((Integer)PriceList.get(k));
232
-
233
- System.out.println("合計金額は"+total+"円です");
234
-
235
-
236
-
237
- }
238
-
239
-
240
-
241
- }
242
-
243
245
  }
244
246
 
245
247
  ```