2013-03-10 25 views
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#   

到目前为止,我有东西,看起来像这样:(有一类称为名称包含了所有客户的个人信息,并包含所有一类叫做地址地址详细信息):


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

你有什么问题? – 2013-03-10 16:54:57

+0

我的程序无法从文本文件中读取。到达ITEM DELIMITER时发生错误。我不确定这是为什么? – 2013-03-10 16:57:56

+0

哪个错误。它有一个错误消息。阅读。或者粘贴在这里。 – 2013-03-10 16:58:42

回答

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

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