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

質問編集履歴

1

追加

2016/02/15 08:37

投稿

akagami_bb
akagami_bb

スコア19

title CHANGED
File without changes
body CHANGED
@@ -9,4 +9,169 @@
9
9
  と記述しますと、
10
10
  もちろんのこと【型 ArrayList<Person> のメソッド add(Person) は引数 (String) に適用できません】とエラーが起きてしまいます。
11
11
  他の場所でArrayListの<Person>を使用していますので、<Person>を<String>に書き換えずに、追加する方法はないのでしょうか?
12
- ご教授の方よろしく御願い致します。
12
+ ご教授の方よろしく御願い致します。
13
+
14
+ 説明不足で申し訳ございません。
15
+ 少しでも不備がないよう下記に全部掲載させていただきます。
16
+
17
+ import java.io.BufferedReader;
18
+ import java.io.BufferedWriter;
19
+ import java.io.File;
20
+ import java.io.FileReader;
21
+ import java.io.FileWriter;
22
+ import java.io.IOException;
23
+ import java.io.PrintWriter;
24
+ import java.util.ArrayList;
25
+
26
+ public class Text {
27
+ // 1人分のデータを格納するクラス
28
+ class Person {
29
+ private int num;
30
+ private String name;
31
+ private String address;
32
+ private String tel;
33
+
34
+ public int getNum(){
35
+ return num;
36
+ }
37
+ public void setNum(int num){
38
+ this.num = num;
39
+ }
40
+ public String getName() {
41
+ return name;
42
+ }
43
+ public void setName(String name) {
44
+ this.name = name;
45
+ }
46
+ public String getAddress() {
47
+ return address;
48
+ }
49
+ public void setAddress(String address) {
50
+ this.address = address;
51
+ }
52
+ public String getTel() {
53
+ return tel;
54
+ }
55
+ public void setTel(String tel) {
56
+ this.tel = tel;
57
+ }
58
+ }
59
+
60
+ private boolean checkBeforeReadFile(File file) {
61
+ if (file.exists()) {
62
+ if (file.isFile() && file.canRead()) {
63
+ return true;
64
+ }
65
+ }
66
+ return false;
67
+ }
68
+
69
+
70
+ // ファイルを読み込む
71
+ public ArrayList<Person> readFile(File file) {
72
+ int i = 0;
73
+ int j =1;
74
+ ArrayList<Person> list = new ArrayList<Person>();
75
+ BufferedReader br = null;
76
+ try {
77
+ if (checkBeforeReadFile(file)) {
78
+ br = new BufferedReader(new FileReader(file));
79
+ String line = null;
80
+ Person person = null;
81
+
82
+ while ((line = br.readLine()) != null) {
83
+ switch (i) {
84
+ case 0:
85
+ // Person クラスのインスタンスを生成
86
+ person = new Person();
87
+ // 名前を格納
88
+ person.setName(line);
89
+ // 次は i = 1 → 住所
90
+ i++;
91
+ person.setNum(j);
92
+ j++;
93
+ break;
94
+ case 1:
95
+ person.setAddress(line);
96
+ i++;
97
+ break;
98
+ case 2:
99
+ person.setTel(line);
100
+ // 1人分のデータの格納が完了したので、リストに追加
101
+ list.add(person);
102
+ i = 0;
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ }
108
+ catch (IOException e) {
109
+ e.printStackTrace();
110
+ }
111
+ finally {
112
+ if (br != null) {
113
+ try {
114
+ br.close();
115
+ }
116
+ catch (IOException e) {
117
+ e.printStackTrace();
118
+ }
119
+ }
120
+ }
121
+ return list;
122
+ }
123
+
124
+ // 全員分のデータを表示する
125
+ void showPeople(ArrayList<Person> list) {
126
+ for (Person person : list) {
127
+ System.out.println("<No." + person.getNum() + ">");
128
+ System.out.println("名前:" + person.getName());
129
+ System.out.println("住所:" + person.getAddress());
130
+ System.out.println("電話番号:" + person.getTel());
131
+ }
132
+ }
133
+
134
+ //コンソール制御
135
+ public static void main(String[] args) {
136
+ try{
137
+ File file = new File("test.txt");
138
+
139
+ BufferedReader br = new BufferedReader(new FileReader(file));
140
+ Text text = new Text();
141
+ ArrayList<Person> list = text.readFile(file);
142
+ text.showPeople(list);
143
+ System.out.println("0:新規登録 1:削除");
144
+ int input = new java.util.Scanner(System.in).nextInt();
145
+
146
+
147
+ if(input == 0){
148
+ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
149
+ System.out.println("名前を入力してください");
150
+ String name = new java.util.Scanner(System.in).nextLine();
151
+ System.out.println("住所を入力してください");
152
+ String address = new java.util.Scanner(System.in).nextLine();
153
+ System.out.println("電話番号を入力してください");
154
+ String tel = new java.util.Scanner(System.in).nextLine();
155
+ //ここでエラー
156
+ list.add(name);
157
+ list.add(address);
158
+ list.add(tel);
159
+ /*pw.println("\n");*/
160
+ pw.close();
161
+ System.out.println("連絡先を追加しました");
162
+ }
163
+
164
+
165
+ if(input == 1){
166
+ System.out.println("削除したい番号");
167
+ int del = new java.util.Scanner(System.in).nextInt();
168
+ list.remove(del);
169
+ System.out.println("No." + del + "のデータを削除しました");
170
+ }
171
+ }
172
+ catch(IOException e){
173
+ System.out.println(e);
174
+ }
175
+ }
176
+
177
+ }