개인/가족 기록이 많은 입력 GEDCOM 파일이 있습니다. 그 목적은 데이터를 다음 형식으로 포맷하는 것입니다 :GEDCOM 파일 읽기 : 입력 파일에서 출력 파일로 정수 변수로 해당 생일, 월, 일을 부분 문자열로 변환하는 방법
이름 (p6, 'Harry Buis'). 출생 (p6, 날짜 (1927,11,17)). 사망 (p6, 날짜 (2001,08,21)). famc (p6, f3). fams (p6, f2).
나는 사람 번호와 이름을 꺼내 출력 파일에 인쇄 할 수 있지만 출생/사망일을 파싱하는 데 문제가 있습니다. 하위 문자열을 사용하여 birthDay, birthMonth 및 BirthYear를 정수로 할당하여 출력 파일에 인쇄 할 수 있기를 원합니다. 날짜별로 정렬 할 수 있도록 정수 여야합니다. 다음은 입력 파일의 한 클라이언트 데이터 샘플입니다.
0 @[email protected] 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 @[email protected]
1 FAMS @[email protected]
그리고 여기가 내가 지금까지 무엇을 내 소스 코드 :
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() 메소드를 사용하면 출력 결과는 다음과 같습니다.
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').
좋은 시작입니다.
하지만 문 경우, 나는이 같은 오류가 발생하는 것을 가지고 있고, 아무 것도 인쇄 할 때 : 이제
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)
, 나는 인덱스 값을 substringing의 모든 조합을 시도하고, 아무것도 작동하는 것 같다 없습니다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까?
미리 감사드립니다.
와우 나는 그 존재를 전혀 몰랐다. 감사! 나는 그것을 시도 할 것이다. –