【发布时间】:2012-05-13 18:52:15
【问题描述】:
我正在尝试编写一个从 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.
【问题讨论】:
-
代码存在一些问题,您能否使用可编译的完整代码清单重新编辑问题。注释掉 Atm 对象实例化,我们不需要它,因为我认为问题出在 for 和 while 循环和扫描仪上。什么时候关闭扫描仪? [1]:docs.oracle.com/javase/tutorial/essential/io/scanning.html
-
好的,现在看看。您仍然需要在扫描仪上调用 close 方法,我认为您需要在处理完交易摘要文件以搜索其中一个 ATM id 后“重新启动”扫描仪。
-
您还可以更新描述您希望将哪些值传递给 ID 为 AAA00022 的 ATM 的 Atm 构造函数的问题吗? (另请注意,输入文件测试数据 id 为 AAA00015 到 AAA00021,而交易报告从 AAA00022 开始。
-
对不起,这是代码不断更改的结果,而且发布的交易报告不完整。我更新了代码
-
无需道歉。我完全明白。您可以对问题进行两项更改,以便轻松回答。更改数据,使报告的单行上没有重复的数字,然后更改最后一条语句,以便描述如何使用报告中的数字调用 Atm ctr。这有意义吗?
标签: java regex java.util.scanner delimiter filereader