我正在尝试遍历我已读入列表的文档,并且需要获取每行中间的特定信息字符串并将该信息字符串打印到一行中,问题是字符串随着每一行的变化而变化,例如读取一堆被其他信息包围的ID
我尝试将列表转换为字符串,并使用doc.substring(doc.lastIndexOf(“ S”)),但这只会获取最后一个ID,并为每行文本打印相同的ID,而不是将特定ID与其他信息。
public static void doStuff(List<String> text) {
char type = 'x';
int bed = 0;
double rentDue = 0;
String ID;
String doc = text.toString();
for (int i = 0; i < text.size(); i++) {
if (text.get(i).charAt(0) == 'S') {
ID = doc.substring(doc.lastIndexOf("S"));
type = text.get(i).charAt(0);
bed = Character.getNumericValue(text.get(i).charAt(text.size() + 4));
rentDue = Payment.processPayment(type, bed);
System.out.print(type);
System.out.print(" ");
System.out.print(bed);
System.out.print(" ");
System.out.print(rentDue);
System.out.print(" ");
System.out.print(ID);
} else {
ID = doc.substring(doc.lastIndexOf("A"));
type = text.get(i).charAt(0);
bed = Character.getNumericValue(text.get(i).charAt(text.size() + 4));
rentDue = Payment.processPayment(type, bed);
System.out.print(type);
System.out.print(" ");
System.out.print(bed);
System.out.print(" ");
System.out.print(rentDue);
System.out.print(" ");
System.out.print(ID);
}
}
}
Expected:
S 3 1352.0 SABQ138
A 2 864.0 AABQ205
S 1 936.0 SABQ127
A 2 864.0 AABQ313
S 2 1144.0 SABQ126
A 2 864.0 AABQ302
Actual:
S 3 1352.0 SABQ126 2, A AABQ302 2]
A 2 864.0 ABQ302 2]
S 1 936.0 SABQ126 2, A AABQ302 2]
A 2 864.0 ABQ302 2]
S 2 1144.0 SABQ126 2, A AABQ302 2]
A 2 864.0 ABQ302 2]