Formatting output file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by newbieBrian
    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.
    For the last little problem, use:

    [code=java]
    field= field.toString( ).replaceAll("[\t"], ",");
    [/code]

    If a field can contain a comma by itself your rules become a bit more complicated.
    You have to use several passes:

    1) replace ", +" by something unique (say XXX) and no white spaces in it;
    2) replace "[ \t]{3,} by ", ", i.e. at least three spaces or tabs by a comma and a space;
    3) if you find [^ ]XXX[^ ], replace it by the same with "s around it.
    4) replace XXX back by a ", "

    maybe you should use one of the freely available CSV processing packages.

    kind regards,

    Jos

    Comment

    • newbieBrian
      New Member
      • Nov 2007
      • 17

      #17
      Originally posted by JosAH
      For the last little problem, use:

      [code=java]
      field= field.toString( ).replaceAll("[\t"], ",");
      [/code]

      If a field can contain a comma by itself your rules become a bit more complicated.
      You have to use several passes:

      1) replace ", +" by something unique (say XXX) and no white spaces in it;
      2) replace "[ \t]{3,} by ", ", i.e. at least three spaces or tabs by a comma and a space;
      3) if you find [^ ]XXX[^ ], replace it by the same with "s around it.
      4) replace XXX back by a ", "

      maybe you should use one of the freely available CSV processing packages.

      kind regards,

      Jos

      Jos,

      Code: ( java )

      1.
      field= field.toString( ).replaceAll("[\t"], ",");

      I got an error message: field is StringBuider type while field.toString is String type. If I use field.toString( )= field.toString( ).replaceAll("[\t"], ",");
      I got this error:
      required: variable
      found : value
      field.toString( ) = field.toString( ).replaceAll("[\t]+",",");

      I will try to figure it out while waiting for your answer.

      Brian
      ^

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        Originally posted by newbieBrian
        Jos,

        Code: ( java )

        1.
        field= field.toString( ).replaceAll("[\t"], ",");

        I got an error message: field is StringBuider type while field.toString is String type. If I use field.toString( )= field.toString( ).replaceAll("[\t"], ",");
        I got this error:
        required: variable
        found : value
        field.toString( ) = field.toString( ).replaceAll("[\t]+",",");

        I will try to figure it out while waiting for your answer.

        Brian
        ^
        My bad; I assumed that 'field' was a String type variable. Use something like this instead:

        [code=java]
        String result= field.toString( ).replaceAll("[\t"], ",");
        [/code]

        kind regards,

        Jos

        Comment

        • newbieBrian
          New Member
          • Nov 2007
          • 17

          #19
          Originally posted by JosAH
          My bad; I assumed that 'field' was a String type variable. Use something like this instead:

          [code=java]
          String result= field.toString( ).replaceAll("[\t"], ",");
          [/code]

          kind regards,

          Jos
          I didn't get the comma either. I will try some thing like:

          char c = inputLine.charA t(i);
          if (!Character.isW hitespace(c))
          {
          field.append(c) ;
          }
          and append the coma when it reaches the whitespace.

          Thanks for your help.

          Brian

          Comment

          Working...