This discussion is archived
3 Replies Latest reply: Jan 4, 2013 1:39 PM by 939520 RSS

group of lines

coco Newbie
Currently Being Moderated

I am new to Java and OOP.
Can you help me with the following problem?
I am reading a file and I need to take/print only the line that start with "#" sign and next line. My problem is that I do not know how to take only these 2 lines.

Thank you.
  • 1. Re: group of lines
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    - find out how to iterate over individual lines of a text file.
    - read Java API how to check if a String starts with a certain substring or how regular expressions can help.
    - if you found a line starting with '#' remember that fact in a boolean type variable.
    - after processing the next line reset that boolean type variable.
    - print lines only if that boolean type variable is true.

  • 2. Re: group of lines
    morgalr Explorer
    Currently Being Moderated
    use an if clause and check the first character of the line... if it's a '#' then print it and the next one too.
  • 3. Re: group of lines
    939520 Explorer
    Currently Being Moderated
    Once you get it working, you should also consider edge conditions:
    1: what if the current and next line both start with #
    2: what if the last line in the file starts with #, and since its the last line, there is no line after it.
    3: what if the file contains no lines that start with #.

    Sorry about complicating your life, but a guy's got to have a hobby ;)


  • Correct Answers - 10 points
  • Helpful Answers - 5 points