3 Replies Latest reply on Jan 4, 2013 9:39 PM by 939520

    group of lines


      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
          - 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
            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
              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 ;)