Results 1 to 6 of 6
  1. #1
    SGRocker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Working with Vector objects + textfile

    Hey guys, I'm intermediate at using Java but I'm kinda stuck on this one thing dealing with Vector objects. What I have is a textfile with people's names in it and 3 attributes belonging to each person, I.E.

    Name 1
    Attribute 1
    Attribute 2
    Attribute 3
    Name 2
    Attribute 1
    Attribute 2
    Attribute 3
    Name 3
    ...
    ...
    ...

    Now let's say that Name 1+their attributes = Guest 1(object 1).....Name 2+their attributes = Guest 2(object 2...etc). What I want to do is put all these different guests, which I guess you can call objects(object consisting of name and attributes), into one big Vector named guests. Now, let's say I didn't know how many Guests I have in my text file (I do, but let's suppose not), what would be the easist way of creating a loop that assigns every 4 lines(Each guest has 1 name and 3 attributes, so it always consists of 4 lines) to a new Guest object?

    My code below only reads the ENTIRE textfile and puts that into one big vector without distinguishing between each guest. I want to implement some kind of loop that assigns every 4 lines to a Guest object and make all those guest objects into a Vector I call Guests. So basically if I have 5 guests, then my Vector Guests, should consist of 5 objects. Those 5 objects being the names and attributes of each guest of course. My end goal for this is if I want to see information about a certain guest, all I would have to do is call Guest 1, and its name and attributes would appear.


    public Vector readFromFile() {

    BufferedReader infile = null;
    String inLine;

    Vector text= new Vector();

    try {
    // Create a file reader
    FileReader fr = new FileReader(filename);
    // Create a buffered stream using fileReader
    infile = new BufferedReader(fr);

    // Read a line and append the line to the text area
    inLine=infile.readLine();
    while (inLine != null) {
    text.add(inLine);
    inLine = infile.readLine();
    }
    } catch (FileNotFoundException ex) {
    System.out.println("File not found: " + filename);
    } catch (IOException ex) {
    System.out.println(ex.getMessage());
    } finally {
    try {
    if (infile != null) infile.close();
    } catch (IOException ex) {
    System.out.println(ex.getMessage());
    }
    }

    return text;
    }


    }

    Any help would be appreciated. Thanks a lot.
    Last edited by SGRocker; 09-16-2008 at 01:04 PM.

  2. #2
    SGRocker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    I forgot to add my code last time, my bad. Here's the updated version.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    You need to define the Guest class with its 4 variables. Then in your read loop, read the next 4 lines, create a Guest object with those 4 lines and add that object to the vector.

  4. #4
    SGRocker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Alright, right now I only have a loop that reads the line so long it's not null but how do I tell it to read just 4 lines and then read another 4 lines etc.?

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

    Default

    Have 4 separate reads in your loop, one for each field.
    Might have a problem if there are not the exact number of records.
    You'll have to check for the condition.

  6. #6
    SGRocker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Alright, appreciate it. I'll give that a try and see what I can come up with.

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 04:02 PM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 PM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM
  5. Vector containing heterogeneous objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 10:19 AM

Posting Permissions

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