Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: New help using Vector class

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default New help using Vector class

    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); ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: New help using Vector class

    have trouble REPLACING the first student on the list
    What Vector class method will "replace" an element in a Vector object?
    Read the API doc and see if any of its methods say they will replace an element.
    use the "set" method, but I'm not quite sure how to do it)
    What does the API doc say for set()?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    new2.java (March 5th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: New help using Vector class

    Okay, I tried and this is what I now have.

    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("Brown","Charlie","123-4567");
     
     
    			line = instream.readLine();
    		}
    		instream.close();
     
    		//CHANGE INFO IN STUDENT CONSTRUCTOR TO HAVE YOUR INFORMATION
    		//TASK 1C
    		Student me = new Student("Wollan", "Jake", "703-6419");
     
    		list.set(0,me);
    		list.removeElementAt(3);
    		list.add(5,s1);
     
     
    		//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
     
    		 for(int index=0; index < list.size(); index++)
    		 System.out.println(list.get(index));
    		}
    	}

    With errors on my set and add methods as well as when I construct my new vector called list.
    It says " vector is raw type" and I need to use "<>"
    Same thing for my add method.
    my set method says my s1 object cant be resolved to a variable.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: New help using Vector class

    With errors
    Please copy the full text of the error messages and paste it here.

    s1 object cant be resolved to a variable.
    Is the definition of s1 in scope (within the same pair of {}s) where it is being used?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: New help using Vector class

    Errors:
    Vector list = new Vector(5);
    Vector is a raw type. References to generic type Vector<E> should be parameterized

    list.set(0,me);
    Type safety: The method set(int, Object) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized

    list.add(5,s1);
    s1 cannot be resolved to a variable

    No it is not. I'm putting the statements where they are istructed to put (//1A 1C ect). I tried putting the add s1 in the same scope, but then i get a tons of errors.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: New help using Vector class

    The definition of s1 must be at the same scope where you refer to it. Now it is hidden within the while() loop.
    The variable s1 will be gone when the loop exits.
    If you create an instance of a Student object inside the loop, what happens to that instance when the loop comes around and the code creates a new instance? Shouldn't each instance be saved after it is created?

    Vector is a raw type. References to generic type Vector<E> should be parameterized
    Read up on how to use generics with collections:
    Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language)
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    new2.java (March 5th, 2013)

  9. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: New help using Vector class

    I'm trying, I really I am I really want to learn this. Can you maybe throw me a bone? I put s1 in the same scope. getting no errors on that. Now I am just getting a out of bounds error.

     
    public class VectorTest
    {
     
    	public static void main(String [] args)throws IOException
    	{
    		// DECLARE AND INSTANTIATE A VECTOR CALLED LIST OF SIZE 5
    		// TASK 1A
     
    		Vector <Student> list = new Vector <Student>(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("Charlie","brown","123-4567");
     
    			//currentStudent = s1;
     
    			line = instream.readLine();
    		}
    		instream.close();
     
    		//CHANGE INFO IN STUDENT CONSTRUCTOR TO HAVE YOUR INFORMATION
    		//TASK 1C
     
     
    		Student me = new Student("Wollan", "Jake", "703-6419");
    		Student s1 = new Student("Charlie","Brown","123-4567");
    		list.set(0,me);
    		list.removeElementAt(3);
    		list.add(5,s1);
     
     
     
    		//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
     
    		 for(int index=0; index < list.size(); index++)
    		 System.out.println(list.get(index));
     
    		}
    	}

    out of bounds exception on my set(me,0).

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: New help using Vector class

    How many elements are in the list? There needs to be at least one to be able to replace the first one.
    Use add() to put elements in the list.

    What does the code do with all the lines of data it reads from the file?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: New help using Vector class

    8 original elements. removed one, and then added one. so 8. Put doesn't the vector just create more space automatically?

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: New help using Vector class

    8 original elements.
    Where are those 8 elements added to the list?

    You missed answering this VERY IMPORTANT question:
    What does the code do with all the lines of data it reads from the file?

    The Vector's capacity will expand as needed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 22nd, 2010, 08:21 AM
  2. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM
  3. [SOLVED] 2D Vector
    By nasi in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 01:42 AM
  4. byte[] from vector
    By perlWhite in forum Collections and Generics
    Replies: 1
    Last Post: August 26th, 2009, 05:10 AM
  5. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM

Tags for this Thread