我有一个文件,其中包含类似的行
barcode date action
例如
IP720H7 20130527192802 in the box
从文件末尾开始,我想搜索包含完全相同条形码的上一行,并采取其日期和操作。
我将在下面给出示例
文件
IP720H4 20130528131526 in refrigerator
IP720H4 20130528130526 in refrigerator
IP720H2 20130528130547 in refrigerator
20IB7 20130528130528 box
IP720H4 20130528130530 in the box
想要的输出
IP720H4 20130528130530 in the box FOUND LAST IP720H4 20130528130526 in refrigerator
20IB7 20130528130528 box NOT FOUND
IP720H2 20130528130547 in refrigerator NOT FOUND
IP720H4 20130528130526 in refrigerator FOUND LAST IP720H4 20130528131526 in refrigerator
IP720H4 20130528131526 in refrigerator NOT FOUND
我尝试使用堆栈以从文件末尾开始搜索,但是在一些 pop() 堆栈变空之后。
while(!lifo.empty())
{
String[] next_line = lifo.pop().toString().split(" ");
//This is the abrcode of the next line
String next_barcode = next_line[0].toString();
//barcode is the one i am trying to find (last of the file)
if (next_barcode.equals(barcode))
{
System.out.println(nnext_barcode + " found");
break;
}
else
{
//NOT FOUND
}
}
但正如我所说,这不是正确的方法,导致堆栈变空。我想逐行搜索,但 STRUCTURE 应该为空,以便继续其他行(最后一行、倒数第二行等)。
我能做些什么?