自分なりに処理の流れをコメントで記入してみました。
その中で、campany = new Campany();の処理をする事で、新しくインスタンスを作成し、一度中身のデータをリセットしているという認識で間違いないでしょうか?
どなたかご教示頂ければと思います。
package kadai;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Strerm {
public static final String TEISU = "会社名"; public static final String TEISU2 = "代表取締役"; public static final String TEISU3 = "設立日"; public static void main(String[] args) { ArrayList<Campany> name = new ArrayList<Campany>(); Campany campany = new Campany(); try { String pass = "/Users/chris/Downloads/facebook.txt"; File file = new File(pass); BufferedReader br = new BufferedReader(new FileReader(file)); String str; while ((str = br.readLine()) != null) { // 文字列を検索して、変数に格納する int comp = str.indexOf(TEISU); int boss = str.indexOf(TEISU2); int data = str.indexOf(TEISU3); int capi = str.indexOf("資本金"); // 戻り値が-1の場合は対象の値が取得出来ていない為、処理を続けずに頭に戻る if (boss == -1) continue; // String kai = "会社名"; // 定数の長さを変数に格納する int comp1 = TEISU.length(); // String dai = "代表取締役"; // 定数の長さを変数に格納する int boss1 = TEISU2.length(); // String setu = "設立日"; // 定数の長さを変数に格納する int data1 = TEISU3.length(); // 文字列を切り出しする為、変数に格納した値を利用して、対象の文字列が取得できる範囲を引数に入れ、値をsetに代入している campany.setKaisya(str.substring(comp + comp1, data));// 会社名が見つかる位置から、会社名(3文字)を足した位置 campany.setName(str.substring(boss + boss1, comp)); campany.setSeturitu(str.substring(data + data1, capi)); // campanyインスタンスから取得した値をリストに詰め込んでいる name.add(campany); // 新しいインスタンを作成して、初期化する **campany = new Campany();** } for (Campany ca : name) { ca.showProfile(); System.out.println(); } br.close(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("コマンドライン引数が設定されていません"); } catch (FileNotFoundException e) { System.out.println("ファイルが見つかりません!!"); } catch (IllegalArgumentException e) { System.out.println("年月日の値が不正です"); } catch (IOException e) { System.out.println(e); } }
}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/05 09:02