bharal
bharal

Reputation: 16164

Java - I'd like to ignore an empty line when reading a file

This should be really really simple, but for whatever reason...

String line;
String question = "";
Question qObj = new Question();
line = br.readLine(); //points to where i am in the file!
if (line == null){
    System.out.println("There was no question here. ");
    System.exit(1);
} else if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
     // do nothing, i don't want empty lines
} else {
    question = line;
}

while ((line = br.readLine())!= null){
    if (line.indexOf(LoadFromDb.ANSWER_BEGIN) == 0){
        dealWithAnswer(br, qObj);
        qObj.setQuestion(question);
        break;
    } else {
    if (!line.isEmpty()){
       question += "\n" + line.trim();
    }
}

If the first line the code above reads is just a blank line, then it adds the blank line to the line object, it doesn't skip it. Any ideas why?

Upvotes: 2

Views: 30496

Answers (3)

Yogi
Yogi

Reputation: 1712

Java8

try(
    Stream<String> stream = Files.lines(
        Paths.get(INPUT_FILE_PATH), 
        Charset.defaultCharset())
){
    stream.map(line -> line.trim()) //Reading line
          .filter(line -> !line.isEmpty()) //Filtering empty lines
          .forEach(System.out::println); //Printing each line
} catch(Exception e) {  
    e.printStackTrace();            
}   

Upvotes: 2

MrLore
MrLore

Reputation: 3780

Regarding my response*, this is the kind of solution I was envisioning:

} else if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
    do {
        line = br.readLine();
    }
    while(line.isEmpty() || line.trim().equals("") || line.trim().equals("\n"));
    question = line;
} else {
    question = line;
}

Although I'm sure there's a more elegant way.

*Setting question to line doesn't appear to change what line is read later (if you're wanting the line to advance before it hits the while loop

Upvotes: 3

DNA
DNA

Reputation: 42597

Works for me, if I understand you correctly:

public class BlankLine
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader("blankline.txt"));
        String line;
        String question = "";
        line = br.readLine();
        if (line == null){
            System.out.println("There was no question here. ");
            System.exit(1);
        } else if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
            System.out.println("Skipped a blank line");
        } else {
            question = line;
            System.out.println("Question="+question);
        }

        // Update: added this to confirm we have skipped a line
        while ((line = br.readLine())!= null){
            System.out.println("Line:"+line);
        }
    }
}

input: a text file with the first line blank, and a second line with the text "Hello World"

output:

Skipped a blank line
Line:Hello World

Upvotes: 2

Related Questions