file1からリストを読み取り、file2でリスト値を検索し、文字列「Total」に達するまでリスト値を含む行の一部をarraylistに追加するメソッドを作成しようとしています。
これがサンプルファイルです1:
AAA00020
AAA00021
AAA00022
AAA00023
AAA00024
AAA00025
AAA00026
file2:
ABC BANK
ATM TRANSACTION SUMMARY
Institution: ABC BANK
Time 13:30:46
Date: 13/05/2012
1000 ATM AAA00022 01 10000.00/ 0.00/ 10000.00 100 289.00 1 0.00 0 0.00 0
2000 ATM AAB00023 02 20000.00/ 0.00/ 20000.00 200 0.00 0 0.00 0 0.00 0
3000 ATM AAC00024 03 30000.00/ 0.00/ 30000.00 300 0.00 0 0.00 0 0.00 0 ________________________________________________________________________________________________________________________________________
Total 60000.00/ 0.00/ 60000.00 600 289.00 1 0.00 0 0.00 0
これが私のコードです:
import java.io.*;
import java.util.*;
public class atmTotals
{
ArrayList <String> atmList = new ArrayList <String>();
ArrayList <Atm> atmUs = new ArrayList <Atm>();
public void getAtmList()
{
try
{
FileReader in = new FileReader("ATMList.txt");
Scanner listIn = new Scanner(in);
while (listIn.hasNextLine())
{
atmList.add(new String (listIn.next()));
}
for (String list : atmList)
{
//System.out.println(list);
}
}
catch( FileNotFoundException fileNotFound)
{
System.err.println( "Error opening file.");
System.exit(1);
}
catch ( NoSuchElementException elementEx)
{
System.err.println( "Incorrect file format.");
System.exit(1);
}
catch ( IllegalStateException stateEx )
{
System.err.println( "Error reading from file.");
System.exit(1);
}
}
public void getAtmTotals()
{
try
{
FileReader file = new FileReader ("ATMTOTAL.rep");
Scanner in = new Scanner (file).useDelimiter("\\s+|/");
for (String list : atmList)
{
while (in.hasNext() && !(in.next().contains("Total")))
{
String currentLine = in.next();
if (currentLine.contains(list))
{
System.out.println("Found String " + currentLine);
atmUs.add ( new Atm (in.next(), in.next(), in.nextFloat()));
}
}
}
//System.out.print(atmUs);
for ( Atm list : atmUs)
{
System.out.printf("%-15s%-3s%10.2f\n", list.getAtmID(), list.getBranchID(), list.getTotalAmt());
}
}
catch( FileNotFoundException fileNotFound)
{
System.err.println( "Error opening file.");
System.exit(1);
}
catch ( NoSuchElementException elementEx)
{
System.err.println( "Incorrect file format.");
System.exit(1);
}
catch ( IllegalStateException stateEx )
{
System.err.println( "Error reading from file.");
System.exit(1);
}
}
}
コンストラクタ:
public class Atm
{
private String AtmID;
private String branchID;
private float TotalAmt;
public Atm ( String AtmID, String branchID, float TotalAmt )
{
this.AtmID = AtmID;
this.branchID = branchID;
this.TotalAmt = TotalAmt;
.....
出力を取得します:
Found String AAA00022
Incorrect file format.