按ID方式搜索:
public class ClientsDetailsList {
public ArrayList <ClientDetails> aListOfClientDetails;
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID)))
found = true;
}
if(found)
return aListOfClientDetails.get(index);
else return null;
}
}
aListOfClientDetails列表格式它读取文件并创建ClientDetails列表。
[IC-x00042W Ms LQ Bethea 205, Willis Road Bolton BO5 1DQ 2000000007 2000100037 2006200319,
IC-x00033D Mr R Bowie 119, Thatcher Way Glasgow GL9 5SX 2006000016 2003100008 2005300001,
IC-x00013A MS GRV Blackwell 209, Drunk Road Hawick HK8 1MY 2006000009 2004100014 2003200304,
IC-x00018O Ms NAP Wallis 244, Grubb Lane Durham DU4 4ZX 2000000006 2003100012 2006200305]
一行是列表的一个对象。使用上面的方法,我尝试返回ID找到的列表的对象(例如,第一令牌IC-x00042W)。但是当我在我的main方法中运行它时,它返回仅第一个对象。(IC-x00042W / Ms / LQ / Bethea / 205,Willis Road / Bolton / BO5 ......) 如果我按另一个对象的id搜索,则返回null。
ClientDetailsList的来源:
txt文件包含以下数据:
IC-x00042W/Ms/LQ/Bethea/205, Willis Road/Bolton/BO5 1DQ/2000000007/2000100037/2006200319#
IC-x00033D/Mr/R/Bowie/119, Thatcher Way/Glasgow/GL9 5SX/2006000016/2003100008/2005300001#
IC-x00013A/MS/GRV/Blackwell/209, Drunk Road/Hawick/HK8 1MY/2006000009/2004100014/2003200304#
IC-x00018O/Ms/NAP/Wallis/244, Grubb Lane/Durham/DU4 4ZX/2000000006/2003100012/2006200305#
IC-x00037N/Miss/DOD/Burke/272, Ambrose Lane/Cambridge/CB2 2XD/2005000003/2001100020#
IC-x00039A/Dr/X/Salter/285, Bannister Road/Sea Palling/SP2 6GW/2000000002/2005100029/2005200306#
IC-x00011I/MR/R/Reece/104, Bannister Lane/Cromer/CR0 6LD/2005000012/2003100001/2001200300#
IC-x00025V/Mr/P/Abbott/163, Drunk Lane/Hunstanton/HU1 1UR/2003000029/2004100017#
IC-x00008L/Dr/P/Runyon/150, Tick Tock Way/Swindon/SW8 4OJ/2004000005/2006100005/2001200316#
IC-x00028F/MR/X/Watt/267, Malton Road/Cambridge/CB4 1PQ/2004100016/2004200312#
IC-x00031X/Mr/S/Lorenz/276, Tick Tock Way/London/LN9 7ID/2005000023/2005100007#
IC-x00020C/Mr/LNV/Mcmillan/44, Drunk Street/London/LN6 1RG/2001000019#
IC-x00015H/Mr/TQZ/Dubose/201, Drunk Road/London/LN4 5RA/2003000026/2006100028/2000200307#
//Creates ClientsDetailsList from source file
public static ClientsDetailsList readFile(File inputFile) throws IOException{
ArrayList <String> clientData = new ArrayList<String>();
ArrayList <ClientDetails> cdList = new ArrayList<>();
ArrayList <PolicyList> arrayofPolsLists = new ArrayList<>();
//Lists of ClientDetails fields
ArrayList <Name> clientName = new ArrayList<>();
ArrayList <String> clientID = new ArrayList<String>();
ArrayList <Address> clientAddress = new ArrayList<>();
// Lists of Name class fields
ArrayList <String> clientTitle = new ArrayList<String>();
ArrayList <String> clientInitials = new ArrayList<String>();
ArrayList <String> clientSurname = new ArrayList<String>();
//Lists of Address class fields
ArrayList <String> clientStreet = new ArrayList<String>();
ArrayList <String> clientCity = new ArrayList<String>();
ArrayList <String> clientPostCode = new ArrayList<String>();
ArrayList <ArrayList <Policy>> list = new ArrayList<ArrayList<Policy>>();
Scanner fileScan = new Scanner(inputFile);
fileScan.useDelimiter("#");
int i =0;
//Reading the file
while(fileScan.hasNext()){
clientData.add(fileScan.next());
Scanner cdScan = new Scanner(clientData.get(i));
cdScan.useDelimiter("/");
ArrayList <String> tokens = new ArrayList<String>();
ArrayList <Policy> clientPolicyNo = new ArrayList<>();
while(cdScan.hasNext()){
tokens.add(cdScan.next());
}
clientID.add(tokens.get(0));
clientTitle.add(tokens.get(1));
clientInitials.add(tokens.get(2));
clientSurname.add(tokens.get(3));
clientStreet.add(tokens.get(4));
clientCity.add(tokens.get(5));
clientPostCode.add(tokens.get(6));
boolean whileController = true;
while(whileController){
clientPolicyNo.add(new Policy(tokens.get(7)));
switch(tokens.size()){
case 9 : clientPolicyNo.add(new Policy(tokens.get(8)));
break;
case 10: clientPolicyNo.add(new Policy(tokens.get(8)));
clientPolicyNo.add(new Policy(tokens.get(9)));
break;
case 11: clientPolicyNo.add(new Policy(tokens.get(8)));
clientPolicyNo.add(new Policy(tokens.get(9)));
clientPolicyNo.add(new Policy(tokens.get(10)));
break;
}
whileController=false;
}
list.add(clientPolicyNo);
i++;
}
//Adding policy lists
for(int j =0; j<clientID.size();j++){
arrayofPolsLists.add(new PolicyList());
arrayofPolsLists.get(j).aListOfPolicies=list.get(j);
}
//Creating Name objects
for(int j =0;j<clientID.size();j++){
clientName.add(new Name());
clientName.get(j).Title = clientTitle.get(j);
clientName.get(j).Initials = clientInitials.get(j);
clientName.get(j).Surname = clientSurname.get(j);
}
//Creating Address objects
for(int j =0;j<clientID.size();j++){
clientAddress.add(new Address());
clientAddress.get(j).street = clientStreet.get(j);
clientAddress.get(j).city = clientCity.get(j);
clientAddress.get(j).postcode = clientPostCode.get(j);
}
//Creating ClientDetails
for(int j =0;j<clientID.size();j++){
cdList.add(new ClientDetails());
cdList.get(j).ClientID = clientID.get(j);
cdList.get(j).fullName = clientName.get(j);
cdList.get(j).fullAddress = clientAddress.get(j);
cdList.get(j).clientsPolicies = arrayofPolsLists.get(j);
}
//Creating a ClientDetailsList object
ClientsDetailsList ClientDetList = new ClientsDetailsList();
ClientDetList.aListOfClientDetails = cdList;
return ClientDetList;
}
ClientDetails类有4个字段:
public String ClientID;
public Name fullName;
public Address fullAddress;
public PolicyList clientsPolicies;
主要方法
File clientsFile = new File("ClientDetailsInput");
InputData e = new InputData();
ClientsDetailsList testList = new ClientsDetailsList();
testList = e.readFile(clientsFile);
System.out.println(testList.getClientDetails("IC-x00013A"));
答案 0 :(得分:2)
在break
之后加found = true;
,例如......
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID))) {
found = true;
break;
}
}
if(found)
return aListOfClientDetails.get(index);
else return null;
}
或者你可以通过完全取消对index
的需求来进一步简化它,例如
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
ClientDetails details = null;
for (ClientDetails check : aListOfClientDetails) {
if(check.ClientID.equals(givenID)) {
details = check;
break;
}
}
return details;
}
<强>更新强>
实际上能够读取数据后,我添加了
String check = aListOfClientDetails.get(index).ClientID;
System.out.println(givenID + " = " + check);
if ((check.equals(givenID))) {
到搜索列表并打印出来......
IC-x00013A = IC-x00042W
IC-x00013A =
IC-x00033D
IC-x00013A =
IC-x00013A
IC-x00013A =
IC-x00018O
IC-x00013A =
IC-x00037N
IC-x00013A =
IC-x00039A
IC-x00013A =
IC-x00011I
IC-x00013A =
IC-x00025V
IC-x00013A =
IC-x00008L
IC-x00013A =
IC-x00028F
IC-x00013A =
IC-x00031X
IC-x00013A =
IC-x00020C
IC-x00013A =
IC-x00015H
...让我吓坏了,直到我意识到这个ID的前缀是一个新的字符......!
所以我所做的就是在read方法的trim
中为每个结果添加tokens
...
clientID.add(tokens.get(0).trim());
(我为每一行做过,只是没有显示),然后导致
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 13, Size: 13
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at testsearch.TestSearch$ClientsDetailsList.getClientDetails(TestSearch.java:47)
at testsearch.TestSearch.main(TestSearch.java:23)
我期望在您的搜索方法中发生这种情况......
如果我们仔细查看搜索循环......
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID)))
found = true;
}
您应该能够看到,无论found
的状态如何,index
在循环结束时总是等于aListOfClientDetails.size()
,因为没有其他退出条件到考虑found
的循环...
这让我回到原来的建议......
永远不要忽视简单System.out.println
声明的强大功能,以检查您的理智和良好的调试器......
答案 1 :(得分:1)
调试会对我有所帮助。
最后你要回来了
return aListOfClientDetails.get(i);
但你应该得到索引“索引”而不是“i”,这是我从未发现过的。
所以用
替换它return aListOfClientDetails.get(index);
当然,当你找到一些东西时,不要忘记离开循环,这样索引就会保持正确的索引。
或者只是在找到对象后立即返回,而不是将找到设置为true。
最后但并非最不重要:我不知道ClienID所以我不能从这里告诉你,但如果它没有实现equals功能,你实际上不会按照你想要的方式工作。所以检查它的功能并覆盖它。
答案 2 :(得分:1)
将扫描仪的分隔符设置为&#39;#&#39;或者&#39; /&#39;导致行分隔符\n
(系统上的任何内容)保留在数据中。因此,一些next()
方法调用最终将生成一个带有前导\n
的String值,如果行结构与&#39对齐,则必然会对来自第二个的所有ID发生这种情况。 ;#&#39;迹象。
您解析的代码非常复杂。我主张读取行(\ n分隔)并一次处理一行,使用
String[] tokens = line.split( "/" );
并将字符串分配给一个对象的目标字段。避免使用许多列表 - 这只会让人感到困惑。