teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

文法の修正

2017/06/07 00:35

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,38 +1,115 @@
1
1
  ###前提・実現したいこと
2
2
  閲覧いただきありがとうございます。
3
3
  未経験のため知識が乏しいのですが、現在入社前研修の一環で、掲題の課題に取り組んでいます。内容としては商品名と価格が事前に入力されているテキストファイル(.txt)を読み込み、正規表現でマッチした商品名をList<String>、価格をList<Integer>に格納。その後、List<Integer>から価格を抽出し、合計金額を算出するといった仕様です。
4
- ファイルの読み込み可否までは書いてみたのですが、正規表現でマッチした文字列を格納する構文が分からない状態です。try-catch文でファイルを一行ずつ読み込んで行き、マッチしなければnullがセットされていくのでは?と考えたのですが上手くいかず。。。
5
- 差し支えなければ例などを挙げていただけると幸いです。。。
4
+ 一通り書てみですが、細かいところがうまくいかず。。。
5
+ 改善点をご指摘頂けると幸いです。
6
6
 
7
7
  ###発生している問題・エラーメッセージ
8
8
 
9
9
  ```Java
10
+ import java.io.BufferedReader;
11
+ import java.io.FileReader;
12
+ import java.io.IOException;
13
+ import java.net.URI;
14
+ import java.nio.file.Files;
15
+ import java.nio.file.Path;
16
+ import java.nio.file.Paths;
17
+ import java.util.ArrayList;
10
- public class Test{
18
+ import java.util.List;
11
- public static void main(String[] args) {
19
+ import java.util.regex.Matcher;
20
+ import java.util.regex.Pattern;
12
21
 
22
+ public class Test{
23
+
13
- File file = new File("c:\\");
24
+ public static void main(String[] args) {
25
+
14
- // exists() メソッドで、ファイル存在しているかどうかを判定
26
+ //ファイル存在有無
15
- if( file.exists());
27
+ if (args.length != 1) {
16
- else
17
- System.out.println("指定のファイルはありません");
28
+ System.err.println("ファイルが開けないか存在しません");
29
+ // エラー
30
+ System.exit(0);
31
+ }
32
+ }
18
33
 
34
+ private URI[] args;
35
+
36
+ //パスオブジェクトをgetメソッドで作成
37
+ Path inputPath = Paths.get(args[0]);{
38
+ if(!Files.exists(inputPath)){
39
+ System.err.println("ファイルが開けないか存在しません。");
40
+
41
+ }
42
+ }
43
+
44
+
45
+
19
- public boolean readTextFileLines( String filepath, ArrayList<String> ProductList ){
46
+ // public String Extraction( String filePath ){
47
+ // }
48
+ String str = "";{
20
- //例外処理の始まり
49
+ // 例外処理の始まり
21
- try{
50
+ try{
51
+ FileReader fr = new FileReader(inputPath);
22
- BufferedReader br = new BufferedReader( fr );
52
+ BufferedReader br = new BufferedReader( fr );
23
-
24
- String line = br.readLine();
25
- while( line != null ) {
26
- ProductList.add( line );
27
- line = br.readLine();
53
+ str = br.readLine();
54
+
55
+ br.close();
56
+
57
+ }catch( IOException e ) {
58
+ e.printStackTrace();
59
+ System.out.println("読み込み不可。");
28
- }
60
+ }
29
- br.close();
61
+ // 読み取った文字列を返す。
62
+ // return str;
30
- }
63
+ }
64
+ //読み込んだ後の文字列に対して正規表現にマッチするかチェックし、
65
+ //マッチしたものから商品名だとか価格だとかを取り出してListに入れたい
66
+ List<String> list;
67
+ List<String> ProductList = new ArrayList<>();{
68
+
69
+ for (int i = 0; i<list.size(); i++){
70
+ String regexProductName = "\\[\\]";
71
+ Pattern pProduct = Pattern.compile(regexProductName);
31
- catch( FileNotFoundException e ){
72
+ Matcher mProduct =pProduct.matcher(list.get(i));
73
+ if(mProduct.find());{
74
+ ProductList.add(mProduct.group(0));
75
+ }else{
76
+ ProductList.add(null);
77
+ System.err.println("抽出不可");
78
+
32
- }
79
+ }
80
+ return ;
81
+
82
+ List<Integer> list;
83
+ List<Integer> PriceList = new ArrayList<>();{
84
+
85
+ for (int j = 0; i<list.size(); i++){
86
+ String regexPriceName = "\\d.+";
87
+ regexPriceName.toString();
88
+ Pattern pPrice = Pattern.compile(regexPriceName);
89
+ Matcher mPrice =pPrice.matcher(list.get(j));
33
- catch( IOException ex ) {
90
+ if(mPrice.find());{
91
+ PriceList.add(mPrice.group(1));
92
+ }else{
93
+ PriceList.add(null);
94
+ System.err.println("抽出不可");
95
+
96
+ return
97
+
34
- }
98
+ }
99
+
35
- return str;
100
+ int sum =0;
101
+ for(int k =0; k<PriceList.size(); k++){
102
+ sum +=((Integer)PriceList.get(k).intValue());
103
+ System.out.println("合計金額は"+sum+"円です");
104
+
105
+ }
106
+
107
+ }
108
+ }
109
+
110
+ }
111
+ }
112
+ }
36
113
  ```
37
114
 
38
115
  ###補足情報(言語/FW/ツール等のバージョンなど)