1 2 Previous Next 16 Replies Latest reply: Apr 1, 2008 10:28 AM by 807601 RSS

    read file and output new file with tabing each new paragraph. please help

    807601
      Hi,

      I need help with this. I'm really stuck.

      I need code for doing the following things.

      I have a file with text in it and I need that file to be read and also need to tab each new paragraph.
      This is how it should look like.

      inFile
      lasjflasjf;lahsglhas;lghsaf
      salfsaj;lfja;sljflasd;fljasdfj
      safasdfasfasfasfsfsfasfasf
      afasfasfasdfsfasdfasfasfsd

      asfasdfasdfsadfasdfasdfas
      asfasdfasdfsafasgawtwetet
      wtwrterggsagsdhhfghdghh
      dfgsdgsdfgdsfgdsfgsdfgdfg

      dsgsdfgsdfgsdgdsfgdsgdfg
      sdgdsfgarerhtsrkjadfg;lgjdg
      dsflgjs;dlfgjsdfjgldsjfgjdfldg

      outFile
      <TAB>asfasdfasfasdfasdfa
      sfsafasfasfalsdfjalsjdfljasdf
      sdfaslfjlasjdfljasdfljasldjfskd

      <TAB> lasflajs;fljasldfjasldj
      asf;alskjfljasdlfjasdklfjsdjfa
      asfalsjdfljsdfljasljlasfjlsjdsdf

      <TAB>lfjlasjflaasldjflasdf
      sdfkjsdjfkjdkfjkjdkfjdjfdkjfdjf
      dkjfkdfdkjfkdkfkdjfkjkdjfkjdf



      Please help. Thank you.

      Edited by: unixsigma on Mar 26, 2008 1:30 AM

      Edited by: unixsigma on Mar 26, 2008 1:31 AM
        • 1. Re: read file and output new file with tabing each new paragraph. please he
          807601
          What did you try? What did you achieve up to now? Where are stuck?
          • 2. Re: read file and output new file with tabing each new paragraph. please he
            807601
            import java.io.*;
            public class Prog3
            {
            public static BufferedReader inFile;
            public static PrintWriter outFile;

            public static void main(String[] args) throws IOException
            {
            String inputString;
            inFile = new BufferedReader(new FileReader("history.dat"));
            outFile = new PrintWriter(new FileWriter("data.out"));
            inputString = inFile.readLine();
            while((inputString=inFile.readLine()) != null)
            {
            if(inputString.length()==0)
            {
            outFile.print(inputString+"\t");
            }
            else
            outFile.print(inputString);
            }
            outFile.close();
            inFile.close();
            }
            }


            This is what I have so far. But it's not working. PLEASE HELP.
            • 3. Re: read file and output new file with tabing each new paragraph. please he
              807601
              Here, try this (make sure your input file is in the same directory as your programm):

              import java.io.*;
              public class Prog3
              {
                   public static BufferedReader inFile = null;
                   public static PrintWriter outFile = null;

                   public static void main(String[] args) throws IOException
                   {
                        String inputString;
                        
                        try
                        {
                             inFile = new BufferedReader(new FileReader("history.dat"));
                             outFile = new PrintWriter(new FileWriter("data.out"));
                             inputString = inFile.readLine();
                             while((inputString=inFile.readLine()) != null)
                             {
                                  if(inputString.length()==0)
                                  {
                                       outFile.print(inputString+"\t");
                                  }
                                  else
                                  {
                                       outFile.print(inputString);
                                  }
                             }
                        }
                        catch(IOException e)
                        {
                             e.printStackTrace();
                        }
                        finally
                        {
                             if(outFile != null)
                             {
                                  outFile.flush();
                                  outFile.close();
                             }
                             if(inFile != null)
                             {
                                  inFile.close();
                             }
                        }
                   }
              }
              • 4. Re: read file and output new file with tabing each new paragraph. please he
                807601
                    inputString = inFile.readLine();
                    while((inputString=inFile.readLine()) != null)
                    { 
                        //...
                    }
                You use readLine() twice here. First, you assign the current line to inputString before entering the loop, and then immediately call readLine() again at the beginning of your loop. This is most likely causing your program to skip over the first line in the input file.
                        if(inputString.length()==0)
                        {
                            outFile.print(inputString+"\t");
                        }
                        else
                            outFile.print(inputString);
                Also, you are using print() here, which will print your string, and cause the next output to be printed on the same line. Try using println() instead; this will print the string to a line, and cause the next output to be printed on its own line.
                • 5. Re: read file and output new file with tabing each new paragraph. please he
                  807601
                  I tried and it doesn't work.
                  the output is the same as I got with my code
                  • 7. Re: read file and output new file with tabing each new paragraph. please he
                    807601
                    The output is the same just with yours it reads line by line like you said. Mine was all in one line before. That's good.

                    However,
                    I need to tab first sentence of each paragraph which I'm not getting.

                    Let me know if you have a solution for this.
                    • 8. Re: read file and output new file with tabing each new paragraph. please he
                      807601
                      Firstly, you would like to tab in after every blank line. This, before writing each line to the output file, you need to check if the previous line was a blank line. This can be done by using a second String object and assiging the current line's value to it at the end of the loop, just before the next line is read, so it is always one line behind.

                      Also, take a look at where you are concatenating your tab, I'm pretty sure you don't want it added to the end of the line!
                      • 9. Re: read file and output new file with tabing each new paragraph. please he
                        DarrylBurke
                        Firstly, you would like to tab in after every blank line. This, before writing each line to the output file, you need to check if the previous line was a blank line. This can be done by using a second String object and assiging the current line's value to it at the end of the loop, just before the next line is read, so it is always one line behind.
                        Or a boolean isLastLineBlank.
                        Also, take a look at where you are concatenating your tab, I'm pretty sure you don't want it added to the end of the line!
                        Yup, I've been thinking that for a long time, but OP should have realized it by now!

                        db
                        • 10. Re: read file and output new file with tabing each new paragraph. please he
                          807601
                          The tab is being concatenated onto the end of a certified empty string, isn't it?
                          • 11. Re: read file and output new file with tabing each new paragraph. please he
                            807601
                            Darryl.Burke wrote:
                            Or a boolean isLastLineBlank.
                            True. I just have a habit of minimizing if statements!
                            • 12. Re: read file and output new file with tabing each new paragraph. please he
                              807601
                              I still didn't get it to work. Does anyone know the solution to this. Thanks
                              • 13. Re: read file and output new file with tabing each new paragraph. please he
                                807601
                                This is working as you told. Plz Try this.

                                import java.io.*;
                                public class Prog3
                                {
                                public static BufferedReader inFile = null;
                                public static PrintWriter outFile = null;
                                
                                public static void main(String[] args) throws IOException
                                {
                                String inputString="";
                                
                                try
                                {
                                inFile = new BufferedReader(new FileReader("history.txt"));
                                outFile = new PrintWriter(new FileWriter("data.txt"));
                                
                                while((inputString=inFile.readLine()) != null)
                                {
                                if(inputString.length()==0)
                                {
                                outFile.print(inputString+"\n\t");
                                }
                                else
                                {
                                outFile.println(inputString);
                                }
                                }
                                }
                                catch(IOException e)
                                {
                                e.printStackTrace();
                                }
                                finally
                                {
                                if(outFile != null)
                                {
                                outFile.flush();
                                outFile.close();
                                }
                                if(inFile != null)
                                {
                                inFile.close();
                                }
                                }
                                }
                                }
                                • 14. Re: read file and output new file with tabing each new paragraph. please he
                                  DarrylBurke
                                  @java_is_easy Please read and take to heart:
                                  [Code Conventions for the JavaTM Programming Language|http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html|Your code is ugly!]

                                  db
                                  1 2 Previous Next