Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    I want to print out the textfile array.

    What I want the code to do is format the array to allow only 1 space between each field, and possibly eliminate the first 4 lines?
    So then I can do further coding to the textfile array.

    The output is below:
    Java Code:
    Active Connections
    
      Proto  Local Address          Foreign Address        State           PID
      TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       508
      TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
      TCP    0.0.0.0:554            0.0.0.0:0              LISTENING       5856
    There are alot more lines below on the output, but thats how it looks like. See the way there are alot of spaces between each field (column)?
    Id like to eliminate the first 4 lines and format the rest of the lines with only 1 space between each field?

    Thanks
    Last edited by Norm; 03-12-2012 at 04:59 PM. Reason: added code tags to preserve formatting

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Reading a text file

    A couple of ways to remove the spaces between the data on each line
    1) use the String class's split() method with a regular expression that skips all spaces. I don't know regexpr and can't help
    2) use the StringTokenizer class

  3. #23
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Reading a text file

    Not split(), but replaceAll. Use a regex that matches multiple whitespace characters and replace with a single space.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #24
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Reading a text file

    Quote Originally Posted by dougie1809 View Post
    Oh yea I have read in the textfile.txt into an array. Ive got one problem, its giving me an error NumberFormatException, which I think its trying to convert the String into an integer, and the first 4 lines do not contain numbers until the 5th line, which is shown below:

    Active Connections

    Proto Local Address Foreign Address State PID
    TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 508
    TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
    TCP 0.0.0.0:554 0.0.0.0:0 LISTENING 5856

    The 'Active Connections' is on the second line, the 3rd is an empty line, and the 4th line containes the headings 'Proto Local Address Foreign Address State PID'. Where the 5th line then starts with the PID's etc.
    So would I have to tell the for loop to start looping from the 5th index of the textfile array? Or how would I get around this problem?
    Thanks

    Im getting the error from this line: int pid = Integer.valueOf(fields[KEY_PID]);
    Yes, you should skip those lines - so long as the info you want always starts on the 5th line. If you can't do that, you can just check the value of the KEY_PID field:
    :: if the value is null (since the first 3 lines have less than 5 columns, the fifth column shouldn't have been assigned any value) then skip that row
    :: if the value is equal to "PID" skip that row

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Reading in a text file
    By TheRealHoff in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2010, 11:47 PM
  3. reading text file
    By trofyscarz in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:24 AM
  4. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 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
  •