大量の個人/家族の記録を含む入力 GEDCOM ファイルがあります。目的は、データを次の形式にフォーマットすることです。
名前(p6、「ハリー・ブイス」)。生年月日(p6、日付(1927,11,17))。死(p6、日付(2001,08,21))。famc(p6、f3)。fams(p6、f2)。
個人番号と名前を引き出して出力ファイルに出力することはできましたが、生年月日と死亡日の解析に問題があります。出力ファイルに出力できるように、substring を使用して、birthDay、birthMonth、および BirthYear を整数として割り当てられるようにしたいと考えています。日付でソートできるように、整数でなければなりません。以下は、入力ファイルからの 1 つのクライアントのデータのサンプルです。
0 @P6@ INDI
1 BIRT
2 DATE 17 Nov 1924
1 NAME Harry /Buis/
1 DEAT Age: 76
2 DATE 21 Aug 2001
1 SEX M
1 FAMC @F3@
1 FAMS @F2@
そして、ここに私がこれまでに持っているもののソースコードがあります:
public class Main {
static Scanner scan;
static BufferedWriter outFile;
static int birthYear = 0;
static int birthMonth = 0;
static String birthDay = "";
static int deathYear = 0;
static int deathMonth = 0;
static int deathDay = 0;
static String name = "";
static String person = "";
static String sex = "";
static String famC = "";
static String famS = "";
static String man = "";
static String woman = "";
static String child = "";
public static void parse() throws IOException {
scan = new Scanner(new FileReader("pbuis.ged"));
outFile = new BufferedWriter(new FileWriter("output.txt"));
String reader = scan.nextLine();
int count = 0;
while (scan.hasNextLine()) {
if (reader.contains("NAME") && count < 1) {
reader = reader.substring(1).replace("/", "");
count++;
System.out.println(reader);
name = reader.replace("NAME", "");
}
if (reader.startsWith("0")) {
person = reader.trim().substring(2, 7).replace("@", "")
.replace("I", "").trim().toLowerCase();
System.out.print(person);
count = 0;
}
if (reader.contains("BIRT")) {
scan.nextLine();
birthDay = Integerreader.substring(6, 9).trim();
}
if (reader.equalsIgnoreCase("") || reader.equalsIgnoreCase(" ")) {
outFile.write("name(" + person + ", " + "'" + name.trim() + "'"
+ ")." + "\n" + birthDay);
}
reader = scan.nextLine();
}
}
public static void main(String[] args) throws IOException {
parse();
}
}
if ステートメント (「BIRT」を含む) がなく、outFile.write() メソッドに「birthDay」がない場合、出力は次のようになります。
name(p1, 'Paul Edward Buis').
name(p2, 'Thomas Edward Buis').
name(p3, 'Jennifer Joy Buis').
name(p4, 'Daniel Paul Buis').
name(p5, 'Barbara Joy VanderWall').
name(p6, 'Harry Buis').
これは良いスタートです。
しかし、そのifステートメントがあると、次のようなエラーが発生し、何も出力されません:
p1Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.substring(Unknown Source)
at Main.parse(Main.java:50)
at Main.main(Main.java:64)
今、部分文字列インデックス値のすべての組み合わせを試しましたが、何もうまくいかないようです。これを修正する方法について何か考えはありますか?
前もって感謝します。