New to using Dynamic Data Structures, Vector classes, and ArrayLists.
-I have a read in file named Student.dat
- I have a class called Student
-I have a class with the main method called VectorTest
I am wondering if I declared and initialized my new vector called list correctly. (1A)
I also am wondering if created a student using data from the line read corrected( almost know this is wrong it also saying I need to add it to the list).
I also am have trouble REPLACING the first student on the list with "me" which is a new object of student. ( I think i need to use the "set" method, but I'm not quite sure how to do it). Any help with this would be appreciated.
Here is my code.
VectorTest:
import java.io.*; import java.util.*; /** *To use a vector as a data structure to store a phone book *Written by Diane Christie for lab use *March 9, 2002 */ public class VectorTest { public static void main(String [] args)throws IOException { // DECLARE AND INSTANTIATE A VECTOR CALLED LIST OF SIZE 5 // TASK 1A Vector list = new Vector(5); String filename = "student.dat"; int comma; Student currentStudent; int count = 0; BufferedReader instream = new BufferedReader(new FileReader(filename)); String line = instream.readLine(); while (line != null) { //String processing, pull apart the line containing // lastName, firstName, phoneNumber // trim() will remove leading spaces from strings comma = line.indexOf(','); String last = line.substring(0, comma).trim(); line = line.substring(comma + 1, line.length()); comma = line.indexOf(','); String first = line.substring(0, comma).trim(); String phone = line.substring(comma + 1, line.length()).trim(); //CREATE A STUDENT USING DATA FROM THE LINE READ IN //ADD IT TO THE LIST //TASK 1B Student s1 = new Student(last,first,phone); line = instream.readLine(); } instream.close(); //CHANGE INFO IN STUDENT CONSTRUCTOR TO HAVE YOUR INFORMATION //TASK 1C Student me = new Student("Wollan", "Jake", "703-6419"); //Task 1D REPLACE THE FIRST STUDENT ON THE LIST WITH me //Task 1E Remove the fourth student on the list. //Task 1F Add Charlie Brown 123-4567 to be the sixth student on the list. System.out.println("Output for the Vector class"); //WRITE A FOR LOOP TO PRINT OUT THE LIST TO THE CONSOLE } }
Student.Java
public class Student { private String lastName; private String firstName; private String phoneNumber; /** * Creates a student using a last name, first name, and phone number * @param last student's last name * @param first student's first name * @param phone student's phone number */ public Student(String last, String first, String phone) { lastName = last; firstName = first; phoneNumber = phone; } /** * Returns student's last name * @return student's last name */ public String getLastName() { return lastName; } /** * Returns student's first name * @return student's first name */ public String getFirstName() { return firstName; } /** * Returns student's phone number * @return student's phone number */ public String getPhoneNumber() { return phoneNumber; } /** * Returns a string of the student's entry for the phone book * @return the student's full name and phone number */ public String toString() { return phoneNumber + " " + firstName + " " + lastName; } }
This the orginal Student.dat (list of students in a note pad file)
Smith, John, 232-1234
Doe, Jane, 232-2345
Hall, Monty, 232-0987
Ketchum, Hank, 232-6789
Jones, Ed, 232-1458
Johnson, Tony, 232-0567
Jones, Amanda, 232-4563
Enockson, Liz, 232-1238
--- Update ---
is this close? list.add(0,me); ?