我试图只在每次循环运行结束时运行 nextLine() ,以便每次迭代都以新行开始循环,但在最后一次运行结束时,我不希望它运行nextLine() 因为它跳过了我程序其余部分所需的数据。我该如何解决这个问题?
String hasInt = fileIn.nextLine();
while(Character.isDigit(hasInt.charAt(0)))
{
data = hasInt.split(",");
info = Integer.parseInt(data[0]);
def = data[1];
case = data[2];
free = data[3];
Case newCase = new Case(info, def, case, free);
miner.addCase(newCase);
hasInt = fileIn.nextLine();
}
将它们全部组合到 for 循环中似乎有效,这让我一路完成了循环,并且似乎有效,但现在在方法开始时它陷入了无限循环
while(fileIn.hasNextLine())
{
if(fileIn.hasNext("[A-Za-z]+"))
{
最佳答案
do {
String hasInt = fileIn.nextLine();
if (!Character.isDigit(hasInt.charAt(0)) {
break;
}
// rest of your code here
} while(true);
关于java - nextLine() 在 while 循环结束时跳过行。丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22246424/