質問編集履歴

1

文法の修正

2017/06/07 00:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  未経験のため知識が乏しいのですが、現在入社前研修の一環で、掲題の課題に取り組んでいます。内容としては商品名と価格が事前に入力されているテキストファイル(.txt)を読み込み、正規表現でマッチした商品名をList<String>、価格をList<Integer>に格納。その後、List<Integer>から価格を抽出し、合計金額を算出するといった仕様です。
6
6
 
7
- ファイルの読み込み可否までは書いてみたのですが、正規表現でマッチした文字列を格納する構文が分からない状態です。try-catch文でファイルを一行ずつ読み込んで行き、マッチしなければnullがセットされていくのでは?と考えたのですが上手くいかず。。。
8
-
9
- 差し支えなければ例などを挙げていただけるです。。。
7
+ 一通り書いみたのですが、細かいところがうまくかず。。。
8
+
9
+ 改善点をご指摘頂けると幸いです。
10
10
 
11
11
 
12
12
 
@@ -16,57 +16,211 @@
16
16
 
17
17
  ```Java
18
18
 
19
- public class Test{
20
-
21
- public static void main(String[] args) {
22
-
23
-
24
-
25
- File file = new File("c:\\");
26
-
27
- // exists() メソッドで、ファイルが存在しているかどうかを判定
28
-
29
- if( file.exists());
30
-
31
- else
32
-
33
- System.out.println("指定のファイルはありません");
34
-
35
-
36
-
37
- public boolean readTextFileLines( String filepath, ArrayList<String> ProductList ){
38
-
39
- //例外処理の始まり
40
-
41
- try{
42
-
43
- BufferedReader br = new BufferedReader( fr );
44
-
45
-
46
-
47
- String line = br.readLine();
48
-
49
- while( line != null ) {
50
-
51
- ProductList.add( line );
52
-
53
- line = br.readLine();
54
-
55
- }
56
-
57
- br.close();
58
-
59
- }
60
-
61
- catch( FileNotFoundException e ){
62
-
63
- }
64
-
65
- catch( IOException ex ) {
66
-
67
- }
68
-
69
- return str;
19
+ import java.io.BufferedReader;
20
+
21
+ import java.io.FileReader;
22
+
23
+ import java.io.IOException;
24
+
25
+ import java.net.URI;
26
+
27
+ import java.nio.file.Files;
28
+
29
+ import java.nio.file.Path;
30
+
31
+ import java.nio.file.Paths;
32
+
33
+ import java.util.ArrayList;
34
+
35
+ import java.util.List;
36
+
37
+ import java.util.regex.Matcher;
38
+
39
+ import java.util.regex.Pattern;
40
+
41
+
42
+
43
+ public class Test{
44
+
45
+
46
+
47
+ public static void main(String[] args) {
48
+
49
+
50
+
51
+ //ファイルの存在有無
52
+
53
+ if (args.length != 1) {
54
+
55
+ System.err.println("ファイルが開けないか存在しません。");
56
+
57
+ // エラー
58
+
59
+ System.exit(0);
60
+
61
+ }
62
+
63
+ }
64
+
65
+
66
+
67
+ private URI[] args;
68
+
69
+
70
+
71
+ //パスオブジェクトをgetメソッドで作成
72
+
73
+ Path inputPath = Paths.get(args[0]);{
74
+
75
+ if(!Files.exists(inputPath)){
76
+
77
+ System.err.println("ファイルが開けないか存在しません。");
78
+
79
+
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+ // public String Extraction( String filePath ){
92
+
93
+ // }
94
+
95
+ String str = "";{
96
+
97
+ // 例外処理の始まり
98
+
99
+ try{
100
+
101
+ FileReader fr = new FileReader(inputPath);
102
+
103
+ BufferedReader br = new BufferedReader( fr );
104
+
105
+ str = br.readLine();
106
+
107
+
108
+
109
+ br.close();
110
+
111
+
112
+
113
+ }catch( IOException e ) {
114
+
115
+ e.printStackTrace();
116
+
117
+ System.out.println("読み込み不可。");
118
+
119
+ }
120
+
121
+ // 読み取った文字列を返す。
122
+
123
+ // return str;
124
+
125
+ }
126
+
127
+ //読み込んだ後の文字列に対して正規表現にマッチするかチェックし、
128
+
129
+ //マッチしたものから商品名だとか価格だとかを取り出してListに入れたい
130
+
131
+ List<String> list;
132
+
133
+ List<String> ProductList = new ArrayList<>();{
134
+
135
+
136
+
137
+ for (int i = 0; i<list.size(); i++){
138
+
139
+ String regexProductName = "\\[\\]";
140
+
141
+ Pattern pProduct = Pattern.compile(regexProductName);
142
+
143
+ Matcher mProduct =pProduct.matcher(list.get(i));
144
+
145
+ if(mProduct.find());{
146
+
147
+ ProductList.add(mProduct.group(0));
148
+
149
+ }else{
150
+
151
+ ProductList.add(null);
152
+
153
+ System.err.println("抽出不可");
154
+
155
+
156
+
157
+ }
158
+
159
+ return ;
160
+
161
+
162
+
163
+ List<Integer> list;
164
+
165
+ List<Integer> PriceList = new ArrayList<>();{
166
+
167
+
168
+
169
+ for (int j = 0; i<list.size(); i++){
170
+
171
+ String regexPriceName = "\\d.+";
172
+
173
+ regexPriceName.toString();
174
+
175
+ Pattern pPrice = Pattern.compile(regexPriceName);
176
+
177
+ Matcher mPrice =pPrice.matcher(list.get(j));
178
+
179
+ if(mPrice.find());{
180
+
181
+ PriceList.add(mPrice.group(1));
182
+
183
+ }else{
184
+
185
+ PriceList.add(null);
186
+
187
+ System.err.println("抽出不可");
188
+
189
+
190
+
191
+ return
192
+
193
+
194
+
195
+ }
196
+
197
+
198
+
199
+ int sum =0;
200
+
201
+ for(int k =0; k<PriceList.size(); k++){
202
+
203
+ sum +=((Integer)PriceList.get(k).intValue());
204
+
205
+ System.out.println("合計金額は"+sum+"円です");
206
+
207
+
208
+
209
+ }
210
+
211
+
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ }
220
+
221
+ }
222
+
223
+ }
70
224
 
71
225
  ```
72
226