私は学校のJavaの課題に取り組んでいます。割り当ては最初の2つのファイルを処理し、2番目のファイルを最初のファイルで調整し、最後に新しいファイルに出力することです。 Records.txt情報の10行(例:######### LASTNAME FIRSTNAME職業)を有している:ことループがファイルの最後に届かない問題が発生しました - 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();
}
}
注 Transactions.txt情報の6行(例有する:「Aを、 D、C '######### lastname firstname occupation)
私が抱えている問題は、ループのタイプに関係なく、私は2つの致命的な状態のうちの1つに到達します。
D C C 386326383スリム菅personalTrainer変更が見つかりませんでした変更...エラースレッド "メイン" Javaで
例外上記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