DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2005
    Posts
    2

    read numbers and string from textfile into a vector

    I am trying to read the below textfile into a vector.
    I have create a class Student, and then I’ve call the file reader to read in line, but then how do I make it read all names, then all numbers and all notes and put it in a vector.
    I am stuck here, any hint most welcome.

    Text file :
    sally
    175
    notes on students
    mark
    180
    notes on students

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    A vector/arraylist deals w. objects. You must define
    a Student class that takes the lines of initial values
    in its constructor like:
    read name,
    read points,
    read notes,
    create Student object w. the three values,
    store in vector (v.addElement(aStudent))
    read name,
    read points,
    read notes,
    create .... etc.

    The you retrieve it like

    Student aStudent=(Student)v.elementAt(i).

    For simplicity you could implement the equals method in
    you Student class to enable lookup in the Vector like
    Code:
    public void boolean equals (Object ob) {
      if (!(ob instanceof Student) return false;
      Student someStudent=(Student)ob;
      return this.name.equals(someStudent.name);
    }
    With this in place you can do like:

    if (v.contains(someStudent)) ....
    or
    int ix=v.indexOf(someStudent);

    Without the implemented equals method the contains and indexOf methods just compares pointer (address) values, and that is ok just for rare occations...
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links