简体   繁体   English

检查readLine是否为空,但不读取行

[英]Check if readLine is blank, but don't read a line

I have a while loop that performs while readLine != "". 我有一个while循环,当readLine!=“”时执行。 The problem is, it reads the line and moves on to the next one, which is not what I want. 问题是,它读取了这一行,然后转到下一个,这不是我想要的。 What I need is either for it to test whether the line is blank without moving on to the next line, or for it to go back to where it was before reading it. 我需要的是要么测试该行是否为空白而不移动到下一行,要么让它返回到读取之前的位置。

Thanks in advance. 提前致谢。

while (fileReader.readLine() != "") {
     String readLine = fileReader.readLine();
}

You provided very little information, so I guess your problem is that you don't have the actual string inside your loop body. 您提供的信息很少,所以我想您的问题是循环体内没有实际的字符串。 Correct me if I'm wrong. 如果我错了纠正我。

To address this problem, you could do: 要解决此问题,您可以执行以下操作:

String line;
while(((line = reader.readLine()) != null) && !("".equals(line)))  {
  System.out.println("line: "+line);
}

In the while loop itself Store the line in a variable 在while循环本身中,将行存储在变量中

String line="";
while((line = br.readLine()) != null)
{
  //Perform your operations
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM