从文本文件分隔符导入数据

时间:2013-03-10 16:48:14

标签: java delimiter

我对尝试从文本文件中读取程序感到困惑。文本文件包括:客户端ID,客户端标题,客户端姓名,客户姓氏,客户端地址,客户城镇或城市,客户邮政编码,政策发布年份和政策编号。其余字段对应于策略编号所属的策略类型。为了分隔每个字段,我被建议给DELIMITER。

文本文件如下所示:

IC-x00042W/Ms/LQ/Bethea/205, Willis Road/Bolton/BO5 1DQ/2007/C02000007/10000/0.5/2008/B27100037/150000/0.3/2011/V30200319/2000/21/1/0#

IC-x00033D/Mr/R/Bowie/119, Thatcher Way/Glasgow/GL9 5SX/2008/L09000016/50000/45/2/2009/C74100008/8000/0.6/2012/B05300001/125000/0.5#

IC-x00013A/MS/GRV/Blackwell/209, Drunk Road/Hawick/HK8 1MY/2013/B09000009/225000/0.1/2011/C14100014/20000/0.1/2010/V63200304/12000/43/5/1#         

到目前为止,我的内容看起来像这样:(有一个名为Name的类,其中包含所有客户端个人详细信息,以及一个名为address的类,其中包含所有地址详细信息):


    final String CLIENT_DELIMITER = "#";
    final String ITEM_DELIMITER = "/";

    Scanner fileScan = null;
    Scanner clientScan = null;

    /*
     * ********************************************************************
     * 
     * Opening the text file and reading from the text file. If the 
     * File cannot be read then an error is thrown
     * 
     * ********************************************************************
     */
    try {
        fileScan = new Scanner(inputFile);
    } catch (Exception e) {
        System.out.println(e);
    }

    //********************************************************************

    String nxtClient = null;


    //Define classes 
    ClientDetailsList clientInfo = null;
    Name clientNameDetails = null;
    Address clientFullAddress = null;
    PolicyList clientPolicies = null;
    ClientDetailsList clientDetails = null;

    ClientDetailsList clientCollection = new ClientDetailsList();

    fileScan.useDelimiter(ITEM_DELIMITER);

    /*
     * ******************************************************************
     * Scan for client details from the text file
     * ******************************************************************
     */

    while (fileScan.hasNext()) {

        // Input the data for the next Client
        nxtClient = fileScan.next().trim();
        clientScan = new Scanner(nxtClient);
        clientScan.useDelimiter(ITEM_DELIMITER);


        //Client Variables
        String clientID = null;
        clientScan.useDelimiter(ITEM_DELIMITER);
        String clientTitle = null;
        String clientInitials = null;
        String clientSurname = null;


        //Reads in details from name
        clientID = fileScan.next().trim();
        clientTitle = fileScan.next().trim();
        clientInitials = fileScan.next().trim();
        clientSurname = fileScan.next().trim();

        //Construct Name
        clientNameDetails = new Name(clientTitle, clientInitials, clientSurname);

        String clientAddress = null;
        String clientTownOrCity = null;
        String clientPostCode = null;
        String policyDetails = null;

        //Reads in details from address
        clientAddress = fileScan.next().trim();
        clientTownOrCity = fileScan.next().trim();
        clientPostCode = fileScan.next().trim();

        //Construct Address
        clientFullAddress = new Address(clientAddress, clientTownOrCity, clientPostCode);

1 个答案:

答案 0 :(得分:0)

nxtClient = fileScan.next();
final String[] srcData = nxtClient.split("/");

if (srcData.length != 8){// 8? am i right?
    System.err.println("wrong data line " + nxtClient);
}

for (int i = 0; i < srcData.length; i++){
srcData[i] = srcData[i].trim();
}

 clientNameDetails = new Name(srcData[0], srcData[1], srcData[2]);
.....

这个比你解决方案看起来更简单