数据集如下所示。
04/01/12#PNW-1234#PA/1234#10
15/01/12#BSE-5566#bT/4674#5@
08/02/12#PNE-3456#Xk/8536#1@
07/03/12#PEA-4567#ZR/7413#3
09/03/12#ESE-6329#HY/7195#30@
03/04/12#ESE-5577#LR/4992#12
23/04/12#PNW-1235#HY/7195#2@
09/05/12#ESE-6329#PV/5732#6
25/05/12#BSE-5566#PV/5732#10@
08/06/12#PNE-3457#kD/9767#1
31/06/12#EMI-6329#ZR/7413#10@
03/07/12#EMI-6329#PV/5732#12
25/07/12#BSE-5566#bT/4674#5@
08/08/12#ENE-5789#DT/9489#8
21/08/12#ESE-6329#PV/5732#30
27/08/12#PEA-4567#PV/5732#3@
11/09/12#ESE-5577#Xk/8536#2
14/09/12#PNW-1235#HY/7195#2@
16/10/12#PSE-3478#bT/4674#1
27/10/12#EMI-6329#kD/9767#10@
19/11/12#ESE-5577#bT/4674#8@
21/12/12#EMI-6329#PV/5732#6@
04/01/13#BNW-4799#kD/9767#12
09/01/13#PNW-1234#ZR/7413#15
25/01/13#BNE-3458#kD/9767#4@
07/02/13#PNE-3457#Au/9271#5
12/02/13#EMI-1267#bT/4674#5@
02/03/13#BSE-5566#Au/9271#25
14/03/13#PSE-3478#PV/5732#3@
04/04/13#ENE-5789#HY/7195#10
16/04/13#PEA-7890#LR/4992#1
25/04/13#BEA-7891#DT/9489#10@
05/05/13#BNW-4799#Xk/8536#2
15/05/13#PSC-1235#ZR/7413#2
30/05/13#PNW-1235#Au/9271#1@
14/06/13#PSE-3478#bT/4674#1
22/06/13#EMI-6329#LR/4992#8@
所以这个方法我从一个文件读入并返回文本文件中@之前的值,因为在@是新月开始的地方。
public static String[] readPurchaseOrderDataFile(Scanner fileScan) {
final String DELIMITER = "@";
try {
ArrayList<String> pbm = new ArrayList();
while (fileScan.hasNext()) {
fileScan.useDelimiter(DELIMITER);
String purchaseOrderByMonth = fileScan.next();
pbm.add(purchaseOrderByMonth);
return pbm.toArray(new String[pbm.size()]);
}
} catch (Exception e) {
System.out.println(e);
}return null;
}
在我的主要方法中,我有这个
for (int i = 0; i < 12; i++) {
String[] textStr = InputFileData.readPurchaseOrderDataFile(pScan); //Brings list in by months
String[] purchaseOrder = textStr[0].split("\\s+");
for (String first : purchaseOrder) {
String[] result = first.split("#");
System.out.println(Arrays.toString(result));
}
由#分隔的每个信息位将存储在数组中。它并没有按照我的意愿存储信息。
答案 0 :(得分:0)
分裂的第一阶段('@'分隔符)很好,你只需要去掉下一个阶段分割的那些空白字符('#'分隔符)。
如果您的文件不是太大而需要严格的代码优化,那么您可以在此方法中移动所有输入文件处理,并将方法更改为返回二维数组(或数组列表)而不是简单/ 1维数组。这样,您将拥有一个表示整个输入数据的结构化对象,然后更容易遍历数据以供以后处理。
public static List<String[]> readPurchaseOrderDataFile(Scanner fileScan) {
final String DELIMITER = "@";
List<String[]> pbm = new ArrayList<String[]>();
try {
fileScan.useDelimiter(DELIMITER);
while (fileScan.hasNext()) {
String purchaseOrderByMonth = fileScan.next();
// replace all white-space with "#" separator character.
String phase1 = purchaseOrderByMonth.replaceAll("\\s+", "#");
String phase2;
if (phase1.length() > 0 && phase1.charAt(0) == '#') {
// get rid of the white-space after the "@" seperator
phase2 = phase1.substring(1);
} else {
phase2 = phase1;
}
pbm.add(phase2.split("#"));
}
} catch (Exception e) {
System.out.println(e);
}
return pbm;
}
示例电话:
List<String[]> textArrs = readPurchaseOrderDataFile(scanner);
for (String[] textArr : textArrs) {
System.out.println(Arrays.toString(textArr));
}
输出:
[04/01/12, PNW-1234, PA/1234, 10, 15/01/12, BSE-5566, bT/4674, 5]
[08/02/12, PNE-3456, Xk/8536, 1]
[07/03/12, PEA-4567, ZR/7413, 3, 09/03/12, ESE-6329, HY/7195, 30]
[03/04/12, ESE-5577, LR/4992, 12, 23/04/12, PNW-1235, HY/7195, 2]
[09/05/12, ESE-6329, PV/5732, 6, 25/05/12, BSE-5566, PV/5732, 10]
[08/06/12, PNE-3457, kD/9767, 1, 31/06/12, EMI-6329, ZR/7413, 10]
[03/07/12, EMI-6329, PV/5732, 12, 25/07/12, BSE-5566, bT/4674, 5]
[08/08/12, ENE-5789, DT/9489, 8, 21/08/12, ESE-6329, PV/5732, 30, 27/08/12, PEA-4567, PV/5732, 3]
[11/09/12, ESE-5577, Xk/8536, 2, 14/09/12, PNW-1235, HY/7195, 2]
[16/10/12, PSE-3478, bT/4674, 1, 27/10/12, EMI-6329, kD/9767, 10]
[19/11/12, ESE-5577, bT/4674, 8]
[21/12/12, EMI-6329, PV/5732, 6]
[04/01/13, BNW-4799, kD/9767, 12, 09/01/13, PNW-1234, ZR/7413, 15, 25/01/13, BNE-3458, kD/9767, 4]
[07/02/13, PNE-3457, Au/9271, 5, 12/02/13, EMI-1267, bT/4674, 5]
[02/03/13, BSE-5566, Au/9271, 25, 14/03/13, PSE-3478, PV/5732, 3]
[04/04/13, ENE-5789, HY/7195, 10, 16/04/13, PEA-7890, LR/4992, 1, 25/04/13, BEA-7891, DT/9489, 10]
[05/05/13, BNW-4799, Xk/8536, 2, 15/05/13, PSC-1235, ZR/7413, 2, 30/05/13, PNW-1235, Au/9271, 1]
[14/06/13, PSE-3478, bT/4674, 1, 22/06/13, EMI-6329, LR/4992, 8]