Formatting output file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbieBrian
    New Member
    • Nov 2007
    • 17

    Formatting output file

    Hi all,

    I am very new to java programming. I want to write a program that turn an input file into the output file with fields separated by comma. Here is the sample input:


    12345 somewords somewords somewords
    21345 somewords somewords somewords

    Below is the output I want to have:

    12345,somewords ,somewords,"som ewords
    21345,somewords ,somewords,some words

    Please help me write this program. I would very much appreciate your help.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by newbieBrian
    Hi all,

    I am very new to java programming. I want to write a program that turn an input file into the output file with fields separated by comma. Here is the sample input:


    12345 somewords somewords somewords
    21345 somewords somewords somewords

    Below is the output I want to have:

    12345,somewords ,somewords,"som ewords
    21345,somewords ,somewords,some words

    Please help me write this program. I would very much appreciate your help.
    Do you know how to read a file using FileReader then.
    Also consider reading the whole file into a StringBuider object and then replacing all spaces with commas. You may need to do a bit of housekeeping at the end.

    Comment

    • dav3
      New Member
      • Nov 2006
      • 94

      #3
      Could you use string tokenizer when reading in the file, then just output token1,token2,t oken3,token4?

      Comment

      • newbieBrian
        New Member
        • Nov 2007
        • 17

        #4
        Originally posted by r035198x
        Do you know how to read a file using FileReader then.
        Also consider reading the whole file into a StringBuider object and then replacing all spaces with commas. You may need to do a bit of housekeeping at the end.
        Hi Admin,

        I know how to read the file using FileReader/BufferedReader. But I have the problem with the logic of how to add comma and replace the whitespaces between each fields. Please if you can write a simple code so that I can learn from it. I appreciate it very much.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by newbieBrian
          Hi Admin,

          I know how to read the file using FileReader/BufferedReader. But I have the problem with the logic of how to add comma and replace the whitespaces between each fields. Please if you can write a simple code so that I can learn from it. I appreciate it very much.
          Open the specs for both the String and StringBuider classes and have a look at the replace and replaceAll methods.

          Comment

          • newbieBrian
            New Member
            • Nov 2007
            • 17

            #6
            Originally posted by r035198x
            Open the specs for both the String and StringBuider classes and have a look at the replace and replaceAll methods.
            Hi Admin,

            Here is what I did:

            import java.io.*;
            import java.util.*;

            public class TestReplaceAll
            {
            public static void main(String[] args) throws Exception
            {

            BufferedReader in = new BufferedReader( new FileReader("sam ple.txt"));
            PrintWriter out = new PrintWriter("ou t.txt");

            String inputLine = null;
            String data = "";
            StringBuilder field = new StringBuilder() ;
            while( (inputLine = in.readLine()) != null)
            {
            for ( int i = 0; i < inputLine.lengt h(); i++ )
            {
            field.append(in putLine.charAt( i));
            data = field.toString( ).replaceAll(" ",",");
            }
            out.write(data) ;

            }
            out.close();
            }

            }

            when I run it, I got this error:

            Exception in thread "main" java.lang.Unsup portedClassVers ionError: Bad version n
            umber in .class file
            at java.lang.Class Loader.defineCl ass1(Native Method)
            at java.lang.Class Loader.defineCl ass(Unknown Source)
            at java.security.S ecureClassLoade r.defineClass(U nknown Source)
            at java.net.URLCla ssLoader.define Class(Unknown Source)
            at java.net.URLCla ssLoader.access $100(Unknown Source)
            at java.net.URLCla ssLoader$1.run( Unknown Source)
            at java.security.A ccessController .doPrivileged(N ative Method)
            at java.net.URLCla ssLoader.findCl ass(Unknown Source)
            at java.lang.Class Loader.loadClas s(Unknown Source)
            at sun.misc.Launch er$AppClassLoad er.loadClass(Un known Source)
            at java.lang.Class Loader.loadClas s(Unknown Source)
            at java.lang.Class Loader.loadClas sInternal(Unkno wn Source)


            Please help me write this program. I am not sure if what I was doing was right or wrong. Thanks.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by newbieBrian
              Hi Admin,

              Here is what I did:

              import java.io.*;
              import java.util.*;

              public class TestReplaceAll
              {
              public static void main(String[] args) throws Exception
              {

              BufferedReader in = new BufferedReader( new FileReader("sam ple.txt"));
              PrintWriter out = new PrintWriter("ou t.txt");

              String inputLine = null;
              String data = "";
              StringBuilder field = new StringBuilder() ;
              while( (inputLine = in.readLine()) != null)
              {
              for ( int i = 0; i < inputLine.lengt h(); i++ )
              {
              field.append(in putLine.charAt( i));
              data = field.toString( ).replaceAll(" ",",");
              }
              out.write(data) ;

              }
              out.close();
              }

              }

              when I run it, I got this error:

              Exception in thread "main" java.lang.Unsup portedClassVers ionError: Bad version n
              umber in .class file
              at java.lang.Class Loader.defineCl ass1(Native Method)
              at java.lang.Class Loader.defineCl ass(Unknown Source)
              at java.security.S ecureClassLoade r.defineClass(U nknown Source)
              at java.net.URLCla ssLoader.define Class(Unknown Source)
              at java.net.URLCla ssLoader.access $100(Unknown Source)
              at java.net.URLCla ssLoader$1.run( Unknown Source)
              at java.security.A ccessController .doPrivileged(N ative Method)
              at java.net.URLCla ssLoader.findCl ass(Unknown Source)
              at java.lang.Class Loader.loadClas s(Unknown Source)
              at sun.misc.Launch er$AppClassLoad er.loadClass(Un known Source)
              at java.lang.Class Loader.loadClas s(Unknown Source)
              at java.lang.Class Loader.loadClas sInternal(Unkno wn Source)


              Please help me write this program. I am not sure if what I was doing was right or wrong. Thanks.
              1.) Use code tags when posting code
              2.) You can use a FileWriter to write to a file
              3.) You can read the whole file into a StringBuider first (note the append method). Then you just call replaceAll once.
              4.) You are using a different version of JRE from your compiler version. Make sure your java path is pointing to the bin folder that contains your compiler version as well.
              5.) Please stop calling me Admin. r035198x will do just fine.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                I'd say use regular expressions on each line of the file. Replace the text "[ \t]+",
                i.e. one or more spaces or tabs with this ", " i.e. a single comma followed by a
                space.

                kind regards,

                Jos

                Comment

                • newbieBrian
                  New Member
                  • Nov 2007
                  • 17

                  #9
                  [PHP]"[/PHP]
                  Originally posted by r035198x
                  1.) Use code tags when posting code
                  2.) You can use a FileWriter to write to a file
                  3.) You can read the whole file into a StringBuider first (note the append method). Then you just call replaceAll once.
                  4.) You are using a different version of JRE from your compiler version. Make sure your java path is pointing to the bin folder that contains your compiler version as well.
                  5.) Please stop calling me Admin. r035198x will do just fine.
                  r035198x, sorry about that.

                  I just fix the version problem and using the FileWriter as you suggested. But I didn't get the right output format. Here is my input file:

                  12345 sometext sometext

                  99 sometext sometext

                  And the output is :
                  12345,sometext, sometext
                  sometext,99,som etext,sometext
                  Please show me how to get it right like below:
                  12345,sometext, sometext
                  99,sometext,som etext

                  Also if I use regex replaceAll("[\t]", ", " I didn't get the comma. Thanks

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by newbieBrian
                    [PHP]"[/PHP]

                    r035198x, sorry about that.

                    I just fix the version problem and using the FileWriter as you suggested. But I didn't get the right output format. Here is my input file:

                    12345 sometext sometext

                    99 sometext sometext

                    And the output is :
                    12345,sometext, sometext
                    sometext,99,som etext,sometext
                    Please show me how to get it right like below:
                    12345,sometext, sometext
                    99,sometext,som etext

                    Also if I use regex replaceAll("[\t]", ", " I didn't get the comma. Thanks
                    Post what you have now. Don't forget the code tags ...

                    Comment

                    • newbieBrian
                      New Member
                      • Nov 2007
                      • 17

                      #11
                      Originally posted by JosAH
                      I'd say use regular expressions on each line of the file. Replace the text "[ \t]+",
                      i.e. one or more spaces or tabs with this ", " i.e. a single comma followed by a
                      space.

                      kind regards,

                      Jos
                      I use the regular expression as you mentioned and I didn't get the comma in.
                      Also, I don't know how to handle the whitespace and the ',' between the text field. Take this input file for example:

                      col1 col2 col3
                      12345 text text someone, sometitle

                      If I use replaceAll space with , then I would end up with

                      12345,text,text ,someone,someti tle. The output would have 5 fields instead of three. Please give me some idea of how to do it. Thanks.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by newbieBrian
                        I use the regular expression as you mentioned and I didn't get the comma in.
                        Also, I don't know how to handle the whitespace and the ',' between the text field. Take this input file for example:

                        col1 col2 col3
                        12345 text text someone, sometitle

                        If I use replaceAll space with , then I would end up with

                        12345,text,text ,someone,someti tle. The output would have 5 fields instead of three. Please give me some idea of how to do it. Thanks.
                        What is the rule for putting the comma then?


                        So the rules are changing ...

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by newbieBrian
                          12345 text text someone, sometitle

                          If I use replaceAll space with , then I would end up with

                          12345,text,text ,someone,someti tle. The output would have 5 fields instead of three. Please give me some idea of how to do it. Thanks.
                          How do you know the first line contains three fields? I'd say there are five already:
                          [12345] [text] [text [someone,] [sometitle]

                          kind regards,

                          Jos

                          Comment

                          • newbieBrian
                            New Member
                            • Nov 2007
                            • 17

                            #14
                            Originally posted by r035198x
                            Post what you have now. Don't forget the code tags ...

                            Please take a look at the code and the input/output files and help me find out what I have been doing wrong here. The regular expression didn't work. The line feed didn't seem to work either. Thanks

                            import java.io.*;
                            import java.util.*;

                            public class TestReplaceAll
                            {
                            public static void main(String[] args) throws Exception
                            {
                            /* sample file:
                            12345 sometext sometext
                            99 someothertext sometexttext

                            unwanted output when I run it:
                            12345 sometext sometext112345 sometext1
                            someothertext19 9 someothertext someothertext
                            */

                            BufferedReader in = new BufferedReader( new FileReader("sam ple.txt"));
                            PrintWriter out = new PrintWriter("ou t.txt");

                            // declare output array to store output record
                            List outputList = new ArrayList();
                            String inputLine = null;
                            String data = "";
                            StringBuilder field = new StringBuilder() ;
                            while( (inputLine = in.readLine()) != null)
                            {
                            for ( int i = 0; i < inputLine.lengt h(); i++ )
                            {
                            field.append(in putLine.charAt( i));
                            if ( inputLine.charA t(i)=='\n')
                            out.write("\n") ;
                            }
                            data = field.toString( ).replaceAll("[\t]+",",");
                            out.write(data) ;

                            }
                            out.close();
                            }

                            }

                            Comment

                            • newbieBrian
                              New Member
                              • Nov 2007
                              • 17

                              #15
                              Originally posted by r035198x
                              What is the rule for putting the comma then?


                              So the rules are changing ...
                              If I have a line with three field, each field may separate by a number of whitespace. some coma may be between one field. For example, the field name:company, which may be like this: companyname, Inc. So it I see something like this, I will put double quote arount it. So it would be "companynam e, Inc". And each field of the input file is separated at least three whitespace. Here is the example:
                              lastname firstname company name
                              foo newbie , sir name, Inc

                              Anyway, I use the regular expression for replaceAll. But it doesn't insert the coma nor replace the whitespace.

                              Example:
                              field.toString( ).replaceAll("[\t"], ",");

                              Thanks for your help.

                              Comment

                              Working...