나는 학교에서 Java 과제를 수행하고 있습니다. 할당은 첫 번째 파일을 읽는 두 파일을 처리하고 두 번째 파일을 사용하여 첫 번째 파일을 조정하고 마지막으로 새 파일로 출력하는 작업입니다. (: ######### LASTNAME FIRSTNAME 점유 예) Transactions.txt 정보 6 개 라인을 갖는다 (예 : Records.txt 정보의 10 행 가지고루프가 파일 끝에 도달하지 않는 문제 - Java
Scanner inputRecords = new Scanner(new File("Records.txt"));
Scanner inputTransactions = new Scanner(new File("Transactions.txt"));
BufferedWriter outputFile = (new BufferedWriter(new FileWriter("NewRecords.txt", true)));
char code; // transaction code
String lineTran = "";
String lineRecord = "";
String midS = "";
String tidS = "";
int tid = 0, mid= 0;
for (int x = 0; x < 6; x++)
{
lineTran = inputTransactions.nextLine();
code = lineTran.charAt(0);
System.out.println(code);
tidS = lineTran.substring(2,11);
tid = Integer.parseInt(tidS);
lineRecord = inputRecords.nextLine();
midS = lineRecord.substring(0,9);
mid = Integer.parseInt(midS);
if (mid < tid) // add a new record lineTran.substring(2,lineTran.length()
{
outputFile.write(lineRecord);
outputFile.newLine();
lineRecord = inputRecords.nextLine();
}
else if (mid == tid)
{
if (code == 'C') //change record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'D') //delete record
{
lineTran = inputTransactions.nextLine();
lineRecord = inputRecords.nextLine();
}
else // add error
{
System.out.println(lineRecord + " Already Exists...add error");
lineTran = inputTransactions.nextLine();
}
}
else
{
if (code == 'A') // add record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'C') // change error
{
System.out.println(lineRecord + " Record already exist...Change Error");
lineTran = inputTransactions.nextLine();
}
else // delete error
{
System.out.println(lineRecord + " Record does not exist...delete error");
lineTran = inputTransactions.nextLine();
}
}
하는 것으로 'A를 D 또는 C '######## 성 성 (firstname occupation)
내가 겪고있는 문제는 루프의 유형에 관계없이 2 가지 중 하나에 도달합니다.
D C C 386,326,383 슬림 칸 퍼스널 트레이너 대한 변경이 발견되지 않음 변경 ... 오류 스레드 "기본"자바
예외 위의 for 루프의 경우 1). lang.StringIndexOutOfBoundsException : 문자열 인덱스가 범위를 벗어났습니다. 0 at java.lang.String.charAt (알 수없는 소스) at fileHandling.main (fileHandling.java:26) 은 결과이며 파일에 쓸 내용이 없습니다.
2) x< 5 루프를 통해 실행하면 프로그램이 정상적으로 실행되지만 마지막 트랜잭션은 건너 뜁니다.
"do while"및 "while"루프를 시도했지만 비슷한 결과 만 나타납니다. 어떤 제안?
* "StringIndexOutOfBoundsException : 문자열 인덱스가 범위를 벗어남 : 0"*에 대해 불분명합니다. 파일에 빈 줄이 있고 코드가이를 처리 할 수 없습니다. – Tom
아니요, 파일을 깨끗하게 설정하고 올바르게 설정했는지 확인했습니다. 두 경우 모두 루프가 도달하지 않는 추가 선이 있다고 확신합니다. – Mark