我正在尝试编写一个从file1读取列表的方法,在file2中搜索列表值并将包含列表值的部分行添加到arraylist,直到达到字符串“Total”。
这是示例文件1:
AAA00020
AAA00021
AAA00022
AAA00023
AAA00024
AAA00025
AAA00026
文件2:
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.