我希望我的应用程序将其信息从一个文本文件更新到另一个文本文件。它在我的代码中的另一个地方工作,但在这里,即使它看起来完全相同,它也不能工作。
程序经历一个while循环,直到文件没有nextLine为止。文件中的前5行是球员得分,后5行是球员姓名。我现在让它在前五个循环中运行得很好,但当涉及到名字时,它就搞乱了:它读入名字1,错过名字2,然后读剩下的3。在这一点上,它中断了for loop并重新启动while循环,因为(我认为)程序知道它漏掉了一行。然后,它读取名称2,并用它覆盖score 1,最后再次尝试读取,此时它会崩溃。
while(!readDone)
{
System.out.println("!Done");
while (readFile.hasNextLine())
{
System.out.println("second While Loop");
for(int j = 0;j<10;j++)
{
if(j<5)
{
System.out.println("For Loop!" + j + "scores");
score[j] = readFile.nextLine();
System.out.println(score[j]);
}
if(j==5 || j>5)
{
System.out.println("For Loop!" + j + "names");
usernames[j-5] = readFile.nextLine();
System.out.println(usernames[j-5]);
}
}
System.out.println("Exit For Loop");
readDone = true;
}
System.out.println("Exit second while");
}
System.out.println("DONE"); 从上面的代码中可以看出为什么会发生这种情况?我正在打印输出到LogCat控制台,所以我知道它缺少名称2。
发布于 2013-02-12 01:03:25
多亏了亨利我找到了问题所在。正如预期的那样,它将项目显示为列表。然而,当我逐个字符地浏览它时,名称后面有一个空格。这就是为什么它遗漏了6行,而打印出了一个空行。
https://stackoverflow.com/questions/14815319
复制相似问题