1

我正在尝试编写一个从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.
4

2 回答 2

0

决定全力以赴,我想出了以下几点。完美运行。

public void getAtmTotals(File file)     
    {

        Scanner rf = null;            
        for (String list : atmList)                         
        {               
        try
        {
            FileReader atmFile = new FileReader(file);
            Pattern p = Pattern.compile ("[\\s/\\s]+");
            rf = new Scanner (atmFile);                                        
            while (rf.hasNextLine())    
            {   
                if (rf.nextLine().contains("Total")) break;
                if (rf.nextLine().contains(list))
                {
                Scanner in = new Scanner(rf.nextLine()).useDelimiter(p);
                while (in.hasNext())                    
                    {                       
                        atmUs.add ( new Atm (in.next(), in.next(), in.next(), in.next(), in.nextFloat(), 
                                in.nextFloat(), in.nextFloat(), in.nextInt(), in.nextFloat(), in.nextInt(), 
                                in.nextFloat(), in.nextInt(), in.nextFloat(), in.nextInt() ) );                                                 
                    }               
                }
            }
        rf.close();
        }

        catch( FileNotFoundException fileNotFound)
        {
            System.err.println( "Error opening  " + file.getName());
            System.exit(1);
        }           

        catch ( NoSuchElementException elementEx)
        {
            System.err.println( "Incorrect " + file.getName() + " format");
            System.exit(1);
        }
        catch ( IllegalStateException stateEx )
        {
            System.err.println( "Error reading from " + file.getName());
            System.exit(1);
        }
        }

        System.out.print(atmUs);
    }
于 2012-05-19T16:43:41.417 回答
0

目前尚不清楚确切的问题是什么,但是当您\r\n在最后一行添加 a 时会出现错误,因为它需要在它之后再添加一行。

不太清楚为什么必须转换它 - Java 应该接受两者......

于 2012-05-13T19:01:31.403 回答