Results 1 to 6 of 6
  1. #1
    NeverHide is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Question Reading txt files and store them in arrays

    Hello this is my first post here,and as you can imagine i'm a java newbie :)

    My problem is this:
    I have 2 txt files that have 3 colums,both files have this format:
    "Packet ID" "Timestamp" "Payload size (Bytes)"
    "0x01f99" 1157715834.7261700 1324
    ... ... ...
    ... ... ...
    (goes on for like 8000 lines or so)

    I want to read the files and store "Packet" ID and "Timestamp" colums.
    I've done some googling and so far i've managed to find how to read the txt file and store it in an array...
    What i want to do is to compare the Packet ID from each file,and if they match i want to substract the equivalent Timestamp of the 2 files.
    I was thinking something like a 2d array,1 colum for packet id and 1 for timestamp,compare the packet id of the 2 arrays and if they match take the timestamp of the same row and substract them.

    Grateful for any coming help :D

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Nah, think OOP and create a Packet class instead.
    Then when you read the lines you create Packet objects that you add to a List<Packet>.

  3. #3
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Instead of using 2D arrays, you can use a more object-oriented approach.

    Write a class like:

    public class PacketObject
    {
    private int packetID;
    private double timeStamp;

    ...getter - setter methods

    public boolean equals( PacketObject other )
    {
    if ( packetID == other.getPacketID ) return true;

    return false;
    }
    }

    You can make 1D arrays of PacketObjects like
    PacketObject[] poarray;

    Then you can iterate through two arrays and compare if they are equal by calling equals() method. And use getTimestamp() getter method to get and substract timestamps.

    BTW, overriding equals method may be logically improper depends on your purpose. You can name it hasSamePacketId() or something like that.

    Hope this helps.
    Last edited by bleah; 05-20-2010 at 01:44 PM. Reason: Syntax correction

  4. #4
    NeverHide is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the quick replies :D
    1 question...
    how will i insert the packetIDs and timestamps in the PacketObject class?I mean what will the imput for the getter-setter methods will be?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    One way would be for the PacketObject class to parse the input lines into the separate fields itself. Pass the constructor the input line to be parsed.
    Or to handle a problem with the format of the data, perhaps it'd be better if the PacketObject class had a static method to do the parsing and throw a exception if there is an error otherwise return a PacketObject with the data that was parsed.

  6. #6
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    I agree with Norm. I wrote it in a such hurry and I forgot to mention the constuctor.
    I believe you can parse your data with a Scanner object using next() and nextInt() and/or nextDouble() method. This static method may also return an array of PacketObjects.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Store values in arrays
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 03-21-2010, 07:53 AM
  3. Replies: 7
    Last Post: 02-02-2010, 11:01 AM
  4. Arrays and File Reading
    By DaFlake in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 05:28 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
  •