0

我正在编写一个“移至前面”编码器,它读取给定的文件,然后将文件解析为列表。它适用于编码,但它只适用于只有一行的文件,我认为问题出在 while 循环中。

这是代码:

while ((line = br.readLine()) !=
       null) // While the line file is not empty after reading a line from teh text file split the line into strings
{
  splitArray = line.split(" ");
}

for (int i = 0; i <= splitArray.length - 1;
     i++) // for every string in the array test if it exists already then output data accordinly
{
  if (FirstPass.contains(splitArray[i])) {
    System.out.println(FirstPass.lastIndexOf(splitArray[i]));
    FirstPass.addFirst(splitArray[i]);
    FirstPass.removeLastOccurrence(splitArray[i]);
  } else if (!FirstPass.contains(splitArray[i])) {
    FirstPass.addFirst(splitArray[i]);
    System.out.println("0 " + splitArray[i]);
  }
}

System.out.println(" ");
for (String S : FirstPass) {
  System.out.println(S);
}
4

2 回答 2

0

您解析 splitArray 的代码在 while 循环之外 .. 所以只有最后一行会被处理。

要处理每一行,请将整个for ()块放在 while 循环中。

while((line = br.readLine()) != null) // While the line file is not empty after reading a line from teh text file split the line into strings
{
    splitArray = line.split(" ");


    for(int i = 0; i <= splitArray.length - 1; i++)     // for every string in the array test if it exists already then output data accordinly
    {
        //..........
    } // end for
} // end while 
于 2013-04-09T23:18:22.800 回答
0

错误位置的大括号:

while((line = br.readLine()) != null) 
{
    splitArray = line.split(" ");
}  // This } shouuldn't be here...
于 2013-04-09T23:18:28.917 回答