1 2 Previous Next 28 Replies Latest reply: Jun 16, 2008 3:42 PM by notivago RSS

    extracting lines from a text file

    807591
      Hi All,
      I need to extract the line Reader Aids from a text file. Logic used is look for the index of DOCID remove the line containing [[Page i]] and grab the next line. The code I have so far is following:
      public static void readLines(String fileName) {
      String line;

      try {
      FileReader file = new FileReader(fileName);
      BufferedReader buffer = new BufferedReader(file);
      while((line = buffer.readLine()) != null) {

      if (line.indexOf("DOCID:")>=0)

      System.out.println(line);
      }
      buffer.close();
      } catch(IOException e) {
      e.printStackTrace();
      }
      }

      The Sample Text is as below:
      [Page i-iii]
      [DOCID:fr03se03-105]

      [[Page i]]

      Reader Aids

      CUSTOMER SERVICE AND INFORMATION
      Thanks in advance.
      M
        • 1. Re: extracting lines from a text file
          807591
          I think I'm missing something, because I didn't see a question mark anywhere in there. What is wrong with that code (besides the fact that you didn't use the "CODE" button to format it)?
          • 2. Re: extracting lines from a text file
            807591
            In my code i have only the index for DOCID but not the desired line of text. At the bottom of my post is the sample text from where to extract the line. I would appreciate if u cud update my code .
            Thanks,
            M
            • 3. Re: extracting lines from a text file
              notivago
              The "logic" you describe seems to differ completely from what you code is doing. Your code just skip all lines that does not contain "DOCID:" and print it out to the screen those that do. What do you really want, can you explain it in plain english?
              As a side note, try not to write if without the {} because although it is right to do so, it is confusing and prone to errors.
              • 4. Re: extracting lines from a text file
                796447
                MinzPinz wrote:
                In my code i have only the index for DOCID but not the desired line of text. At the bottom of my post is the sample text from where to extract the line. I would appreciate if u cud update my code .
                Thanks,
                M
                Contrary to popular belief (it seems), this is not the place to dump your unfinished homework assignment to do for you.
                • 5. Re: extracting lines from a text file
                  notivago
                  MinzPinz wrote:
                  In my code i have only the index for DOCID but not the desired line of text. At the bottom of my post is the sample text from where to extract the line. I would appreciate if u cud update my code .
                  Thanks,
                  M
                  What do you mean by extract it? Do you want it erased from the file? Do you want to display it on screen? Or do you want to display everything else? You can't solve a problem if you haven't clearly understood it.
                  • 6. Re: extracting lines from a text file
                    807591
                    I am trying to display the line which says "Reader Aids". For doing that I am looking for DOCID and the first line of text after that. We will have to also remove "[[Page i]]" which we can by using regular expressions. I hope I am clear this time.
                    • 7. Re: extracting lines from a text file
                      807591
                      FYI..I am asking for help and not dumping my unfinished work.
                      • 8. Re: extracting lines from a text file
                        807591
                        MinzPinz wrote:
                        I would appreciate if u cud update my code .
                        MinzPinz wrote:FYI..I am asking for help and not dumping my unfinished work.
                        So which part of it are you willing to do?
                        • 9. Re: extracting lines from a text file
                          807591
                          I can remove the "[[Page i]]" but I am not able to display the desired line which is the line which says "Reader Aids". I'll really appreciate if u cud help.
                          • 10. Re: extracting lines from a text file
                            notivago
                            Posting your code with code tag(try to use it from now on along with proper formating):
                            import java.io.BufferedReader;
                            import java.io.FileReader;
                            import java.io.IOException;
                            
                            public class ReaderAid {
                                 public static void readLines(String fileName) {
                                      String line;
                            
                                      try {
                                           FileReader file = new FileReader(fileName);
                                           BufferedReader buffer = new BufferedReader(file);
                                           while((line = buffer.readLine()) != null) {
                            
                                                if (line.indexOf("DOCID:")>=0)
                                                     System.out.println(line);
                                           }
                                           buffer.close();
                                           
                                      } catch(IOException e) {
                                           e.printStackTrace();
                                      }
                                 } 
                            }
                            If you look at your code you will see that the only time you print something, is when you find the doc line. Thats why I asked for clarification, your code says something different from what you are saying. Besides that you haven't specified any rule regarding the [[page i]] exclusion, also how is docid related to the line of reader aids? Try to write the file as you expect the output, and comment on why you took out a line or left it to display.
                            • 11. Re: extracting lines from a text file
                              807591
                              This is new Sample text:
                              /**
                              [Notices]
                              [Page 11405]
                              [DOCID:fr07mr06-81]
                              -----------------------------------------------------------------------

                              DEPARTMENT OF THE INTERIOR

                              Bureau of Land Management

                              [WY-920-1310-01; WYW144809]

                              Wyoming: Notice of Proposed Reinstatement of Terminated Oil and
                              Gas Lease WYW144809 */

                              Look at the above sample text.
                              1) I need to display the line in bold (Bureau of Land Management) which is the title for this sample text.
                              2) For this I need to find index of DOCID , remove ------------------------------------------------- line, remove uppercase line and then grab the next line of text.
                              3) As the title line is always found after DOCID, this is the logic I think is the best way to grab the desired line
                              4) In my code I have only the index of DOCID, after that I am not able to go furthur. Pls ignore printing of the line .

                              I wud really appreciate ur help asap as i am running out of time. Let me know if u have anymore questions
                              • 12. Re: extracting lines from a text file
                                796447
                                MinzPinz wrote:
                                This is new Sample text:
                                ...
                                Look at the above sample text.
                                1) I need to display the line in bold (Bureau of Land Management) which is the title for this sample text.
                                2) For this I need to find index of DOCID , remove ------------------------------------------------- line, remove uppercase line and then grab the next line of text.
                                3) As the title line is always found after DOCID, this is the logic I think is the best way to grab the desired line
                                4) In my code I have only the index of DOCID, after that I am not able to go furthur. Pls ignore printing of the line .
                                Yes yes yes we know you have a homework assignment to complete and this goes towards describing what needs to be implemented.
                                And again, you're not asking any specific JAVA PROGRAMMING related questions, you're just dumping the (unfinished) homework on us to finish for you.

                                NO

                                >
                                I wud really appreciate ur help asap as i am running out of time.
                                That's just too darn bad
                                • 13. Re: extracting lines from a text file
                                  807591
                                  If u do not want to help, fine with me but stop commenting! Don't waste ur time.
                                  • 14. Re: extracting lines from a text file
                                    807591
                                    MinzPinz wrote:
                                    If u do not want to help, fine with me but stop commenting!
                                    It's not within your purview to govern the responses of others.
                                    Don't waste ur time.
                                    It's been a long time since [Ur time|http://en.wikipedia.org/wiki/Ur].

                                    ~
                                    1 2 Previous Next