Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Rep Power

    Default [SOLVED] Extract certain data from file, help

    Hi, I'm trying to extract the ASCII code and probability from a text file whose lines look like this for example:

    31 0 0.000000
    32 158380 0.153918
    33 72 0.000070
    34 1977 0.001921
    35 4 0.000004
    36 4 0.000004

    .. etc. I'm trying to extract just the first and third columns. I'm a little confused as to how do I do this and store them to their appropriate variables. Can I use a tokenizer even though I'm dealing with chars and floats? Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Ultimately, you are only dealing with String here. Myself, I'd read a line, then split it on space, then I'd parse the numbers I needed with the appropriate parse method.

  3. #3
    Join Date
    Mar 2009
    Rep Power


    Ah, thank you. I understand, however I'm still a little fuzzy on the syntax. When I read in the line until I hit a space, and set it to the appropriate variable after parsing, how do I continue from where I left off in the line and parse the next set of information from there?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Java Code:
    String line = {whatever}.readLine();
    String[] splits = line.split("\\p{space}");
    'splits' is now an array containing all elements separated by whitespace (i.e., it is a 3 element array in this example).

  5. #5
    PatrickMc is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default Prints columns 1 and 3 from a data file

    I often use biterscripting ( for free download ) whenever I don't have time to write code in Java. If this task of printing columns 1 and 3 is a stand-alone task, the following biterscripting code will do the job.

    Let's assume the name of the data file is X.

    var str data ; cat X > $data
    while ( $data <> "")
    var str line ; lex "1" $data > >$line # Get next line
    wex "1" $line # Print the 1st column
    wex "3" $line # Print the 3rd column
    Hope this helps.

    (Send me email if your requirements are more complex.)

Similar Threads

  1. Extract A tar.gz file
    By sajdutt in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2009, 09:29 AM
  2. Replies: 3
    Last Post: 12-09-2008, 08:17 AM
  3. Java Extract PDF data from location XY
    By Unite in forum Advanced Java
    Replies: 1
    Last Post: 06-30-2008, 01:31 PM
  4. [SOLVED] How to Extract Data From this text file?
    By jazz2k8 in forum New To Java
    Replies: 31
    Last Post: 04-18-2008, 10:45 AM
  5. Extract Text from PDF File using java
    By TSW1016 in forum Advanced Java
    Replies: 5
    Last Post: 01-06-2008, 11:03 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