Java 搜索文件中的字符串并将某些行值添加到ArrayList

Java 搜索文件中的字符串并将某些行值添加到ArrayList,java,regex,java.util.scanner,delimiter,filereader,Java,Regex,Java.util.scanner,Delimiter,Filereader,我正在尝试编写一个方法,从file1中读取列表,在file2中搜索列表值,并将包含列表值的部分行添加到arraylist,直到达到字符串总数 以下是示例文件1: AAA00020 AAA00021 AAA00022 AAA00023 AAA00024 AAA00025 AAA00026 文件2: ABC BANK ATM TRANSACTION SUMMARY Institution: ABC BANK

我正在尝试编写一个方法,从file1中读取列表,在file2中搜索列表值,并将包含列表值的部分行添加到arraylist,直到达到字符串总数

以下是示例文件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);
        }
    }               
}
我得到输出:

Found String AAA00022
Incorrect file format.

现在还不清楚确切的问题是什么,但在最后一行添加\r\n时会出现错误,因为后面需要另一行


我不太清楚为什么要转换它-Java应该同时接受这两种语言…

决定将我的头放在它上面,我提出了以下建议。工作完美

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);
    }

代码中有几个问题,您可以使用编译的完整代码列表重新编辑问题。注释掉Atm对象实例化,我们不需要它,因为我认为问题在于for和while循环和scanner。什么时候关闭扫描仪?[1] :好的,现在看看这个。您仍然需要在扫描器上调用close方法,我认为您需要在处理事务摘要文件后重新启动扫描器,搜索其中一个ATM ID。此外,您是否可以更新问题,描述您希望将哪些值传递到ID为AAA00022的ATM的ATM构造函数中?另请注意,输入文件测试数据id为AAA00015到AAA00021,而交易报告是从AAA00022开始的。很抱歉,这是代码持续更改的结果,并且过账的交易报告不完整。我更新了密码,不需要道歉。我完全理解。你可以对这个问题做两个修改,这样它就很容易回答了。更改数据,使报告的单行上没有重复的数字,然后更改最后一条语句,以便描述如何使用报告中的数字调用Atm ctr。这有意义吗?但不要这样做,\r\n因为那样会破坏可移植性。Do System.getPropertyline.separator;
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);
    }