Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Smile [SOLVED] How to Extract Data From this text file?

    I am Having a .csv file Data Like thsi:

    "Name: Jazz Cust ID:969696"
    "Invoice:586969"
    "Date:"
    "11-03-2008"
    "WorkorderNo:"
    "8238288"
    "PMC ACTIVITY PLAN"
    "SL","Activities","Description","Input","Outpu t"

    "Amount:","5000"
    :confused::rolleyes:
    Here the data is Top Down structure..Like..WorkOrderNo will be first line the Result will be in next line...

    Now i want to extract the "Date:" and "WorkOrder:" two fields.
    The Output:
    Date:11-03-2008
    WorkoderNo:8238288

    I have tried with Regular Expressions for this..But if in that it may have another Date then,it is also taking that one.SO any java code for this????

    It is very tough ask so far in my career...Try this if u can...

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hello jazz2k8,

    Will the input .csv file always be in this order?

    You could read in this file line by line and add each line to an Array. Then you could output the Arrays in the order that you want them..
    Did this post help you? Please me! :cool:

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Something like this:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    public class csv {
    
    public static void main(String[] args) throws Exception {
    
    // Set file name & path		
    String filepath = "file.csv";
    		
    // Read in file
    FileInputStream in = new FileInputStream(filepath);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    
    // Declare Array which will hold 10 lines				
    String[] myarray;
    myarray = new String[9];
    
    // Read each line into the array			
    for (int i = 0; i < myarray.length; i++){
    myarray[i] = br.readLine();
    }
    
    in.close();
    
    // Print out array info in desired order. Delete " characters		
    System.out.println(myarray[2].replace("\"", "") + myarray[3].replace("\"", ""));
    System.out.println(myarray[4].replace("\"", "") + myarray[5].replace("\"", ""));
    
      }
    }
    Output:

    Java Code:
    Date:11-03-2008
    WorkorderNo:8238288
    Last edited by DonCash; 04-15-2008 at 04:32 PM.
    Did this post help you? Please me! :cool:

  4. #4
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Lightbulb Hi

    Thanks for your Reply.

    My .csv file may vary...but i need toextract those same particular fields for every file.
    Can you generate code as generic
    thanks,
    jazz

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Will

    "Date:"
    "11-03-2008"
    "WorkorderNo:"
    "8238288"

    Always be in this order?
    Did this post help you? Please me! :cool:

  6. #6
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Default

    Order means ....
    "Date:"
    "11-03-2008"
    bla...bla..bla.
    -
    -
    -----
    "WorkorderNo:"
    "8238288"
    ...
    it will be like this also...we need to match with Date and WorkOrder...where ever it may be...

  7. #7
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by jazz2k8 View Post

    I have tried with Regular Expressions for this..But if in that it may have another Date then,it is also taking that one.
    Is this other Date that your regular expression is picking up also labeled "Date" or does it just have the word date in it causing this problem.

    Also, is there going to be one Date/WorkOrder pair per file or are there multiple entries you want to look for all of them or just one?

  8. #8
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Chris.Brown.SPE View Post
    Is this other Date that your regular expression is picking up also labeled "Date" or does it just have the word date in it causing this problem.

    Also, is there going to be one Date/WorkOrder pair per file or are there multiple entries you want to look for all of them or just one?
    I validated Date using RegularExpression..but the problem i faced is that if i have another date in my .csv file then i am getting problem.I want only The invoice Date and workOrder how many times it may occur but i need only one.

  9. #9
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    So the invoice date is: "Date:"

    Is the other date in the exact same format? How can you tell the two apart?
    Did this post help you? Please me! :cool:

  10. #10
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Thumbs up

    Here is my actual .csv file...
    ----------------------------------------------------------------------
    "Kala Genset Pvt.Ltd."
    "392/1, Mahelunge Ingale, ChaRan - Talegaon Road, Taluka - Khed, District - Une, H No: 02135 - 259120"
    "Consignee"
    "Aster Teleservices Pvt.Ltd-ECP - Kar - C No 6/1,2nd Floor,16th Cross, GDP-Extension,Vyalikaval,"
    "Malleswaram,Bangalore - 3"
    "E-mail : satya@atspl.com"
    "INVOICE NO Dated"
    "Feb 08/Purc/18 12-Feb-2008"
    "Supplier's Ref Other Reference(s)"
    " 2846"
    "Buyer's Order No. Dated"
    "ATSPLJECP/KAR/083 24-Jan-2008"

    ---------------------------------------------

    IN this i am Having Two "Dated" Label Fields.One is of Invoice Date another is of Buyers Order Date"...
    Any Help???


    Note:When i see the preview the Invoice No and Dated are seems like they are combined actually there will be spacein between those.

  11. #11
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Where is the WorkorderNo in this file???

    I cant see it in the above example.
    Last edited by DonCash; 04-15-2008 at 04:33 PM.
    Did this post help you? Please me! :cool:

  12. #12
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by DonCash View Post
    Where is the WorkorderNo in this file???

    I cant see it in the above example.

    Actually my requirement is
    "INVOICE NO Dated"
    "Feb 08/Purc/18 12-Feb-2008"
    "Buyer's Order No. Dated"
    "ATSPLJECP/KAR/083 24-Jan-2008"

    Form this i want
    INVOICE NO:Feb 08/Purc/18
    Dated:12-Feb-2008
    Buyer's Order No:ATSPLJECP/KAR/083
    Dated:24-Jan-2008
    Note:The fields are not in fixed fomat..they can be anywhere in the file.

  13. #13
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    This gets more complicated by the minute lol

    I know they can be anywhere in the file but will

    "INVOICE NO Dated"
    "Feb 08/Purc/18 12-Feb-2008"

    and

    "Buyer's Order No. Dated"
    "ATSPLJECP/KAR/083 24-Jan-2008"

    always be together like that?
    Last edited by DonCash; 04-16-2008 at 11:22 AM.
    Did this post help you? Please me! :cool:

  14. #14
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Default

    Ummmm...for right now taking this as Same...:confused:

  15. #15
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    OK this was quite hard to get my head around but this is what I have come up with:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    public class csv {
    
    public static void main(String[] args) throws Exception {
    		
    // File path & name
    String filepath = "file.csv";
    		
    // Read in file to determine how many lines it has
    FileInputStream in = new FileInputStream(filepath);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    int counter = 0;
    		
    while ((strLine = br.readLine()) != null)
    {
    counter++;
    }
    			
    in.close();
    		
    // Read in file
    FileInputStream in2 = new FileInputStream(filepath);
    BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
    String strLine2;
    		
    // Declare array
    String[] myarray;
    myarray = new String[counter];
    			
    // Add each line to the array
    for (int i = 0; i < myarray.length; i++){
    myarray[i] = br2.readLine();
    }
    		
    in2.close();
    		
    // Loop through array looking for the correct text, format & print
    for (int c = 0; c < myarray.length; c++){
    	
    if(myarray[c].contains("INVOICE NO")){
    System.out.println("INVOICE NO: " + myarray[c+1].replace("\"", "").substring(0, 15));
    System.out.println("Dated: " + myarray[c+1].replace("\"", "").substring(15, 26));
    }
    
    if(myarray[c].contains("Buyer's Order")){
    		
    String[] split = myarray[c+1].split(" ");
    				
    System.out.println("Buyer's Order No: " + myarray[c+1].replace("\"", "").substring(0, 18).trim());
    System.out.println("Dated: " + split[1].replace("\"", ""));
        }
      }
     }
    }
    Output:

    Java Code:
    INVOICE NO: Feb 08/Purc/18 
    Dated: 12-Feb-2008
    Buyer's Order No: ATSPLJECP/KAR/083
    Dated: 24-Jan-2008
    This will be the output no matter where

    "INVOICE NO Dated"
    "Feb 08/Purc/18 12-Feb-2008"

    and

    "Buyer's Order No. Dated"
    "ATSPLJECP/KAR/083 24-Jan-2008"

    are located in the csv file. :o
    Did this post help you? Please me! :cool:

  16. #16
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Red face

    Thanks for your time and work ..i will try this..let you know if i have still...

    Thanks

  17. #17
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Red face

    Can we try more generic form..

  18. #18
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Please post a FULL example of the csv file. No one can help you without it.
    Last edited by DonCash; 04-17-2008 at 02:50 PM. Reason: bla bla bla
    Did this post help you? Please me! :cool:

  19. #19
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Thumbs up

    In the Above Code problem is .. space between the fields..so i am attaching it..please have a look...One more imp thing is ..The Fields are same but the space may vary in between in those("invoice No. Dated")..:-)
    Attached Files Attached Files
    Last edited by jazz2k8; 04-17-2008 at 07:24 AM.

  20. #20
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    This is going to be very hard to do if the spacing between the information is not constent. How much of a difference is there between files? Can you attach another few examples for me to test?
    Last edited by DonCash; 04-17-2008 at 12:01 PM.
    Did this post help you? Please me! :cool:

Page 1 of 2 12 LastLast

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  3. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 11:30 AM
  4. Extract Text from PDF File using java
    By TSW1016 in forum Advanced Java
    Replies: 5
    Last Post: 01-07-2008, 12:03 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •