2

我对试图让我的程序从文本文件中读取有点困惑。文本文件包括:客户 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);
4

1 回答 1

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]);
.....

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

于 2013-03-10T17:02:02.480 回答