Results 1 to 7 of 7
  1. #1
    xHoly is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default [SOLVED]Need Help Regarding Vectors

    Hi guys, I'm new to this forums and currently doing a task by my school.
    So far so good, but there's something I can't figure it out, it's like this,

    Java Code:
    import java.util.*;
    
    public class TestVector3 {
    
        public static void main(String[] args)
        {
    		IdolFinalist kathy = new IdolFinalist("Kathy Tan", 200);
    		IdolFinalist nadia = new IdolFinalist("Nadia Lim", 350);
    		IdolFinalist dan = new IdolFinalist("Dan Osman", 150);
    		
    		Vector v = new Vector();
    		v.add(kathy);
    		v.add(nadia);
    		v.add(dan);
    		
    		System.out.println(v.get(1));
    		for(int i = 0; i<v.size(); i++)
    		{
    			IdolFinalist temp = v.get(i);
    			System.out.println(temp.getName() + temp.getVotes()); 
    		} 
        }
    }
    I can't seem to get my vector values to be stored in a dummy Object at this line IdolFinalist temp = vec.get(i);
    Any help will be appreciated thanks.
    Last edited by xHoly; 05-23-2010 at 07:57 AM.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Java Code:
    			IdolFinalist temp = v.get();
    The get() method needs an int index -- you need to tell it which element of the Vector you want to get. (Hint: you probably want your i variable.)

    -Gary-

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

    Default

    Quote Originally Posted by gcalvin View Post
    Java Code:
    			IdolFinalist temp = v.get();
    The get() method needs an int index -- you need to tell it which element of the Vector you want to get. (Hint: you probably want your i variable.)

    -Gary-
    Hey thanks for the quick reply. Yea I did that but it shows me incompatiable type. I got stuck here.

    Sorry forgot to update the code.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Where do you get incompatible type? Show us the exact code you are trying to compile and run, and give us your exact error message please.

    -Gary-

  5. #5
    xHoly is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    public class IdolFinalist
     {	
     	private String name;
     	private int votes;
    
        public IdolFinalist() 
        {
        	name = null;
        	votes = 0;
        }
        
        public IdolFinalist(String n, int v)
        {
        	name = n;
        	votes = v;
        }
        
        public String getName()
        {
        	return name;
        }
        
        public void setName(String n)
        {
        	name = n;
        }
        
        public int getVotes()
        {
        	return votes;
        }
        
        public void setVotes(int v)
        {
        	votes = v;
        }
     }
    Java Code:
    import java.util.*;
    
    public class TestVector3 {
    
        public static void main(String[] args)
        {
    		IdolFinalist kathy = new IdolFinalist("Kathy Tan", 200);
    		IdolFinalist nadia = new IdolFinalist("Nadia Lim", 350);
    		IdolFinalist dan = new IdolFinalist("Dan Osman", 150);
    		
    		Vector v = new Vector();
    		v.add(kathy);
    		v.add(nadia);
    		v.add(dan);
    		
    		System.out.println(v.get(1));
    		for(int i = 0; i<v.size(); i++)
    		{
    			IdolFinalist temp = v.get(i);
    			System.out.println(temp.getName() + temp.getVotes()); 
    		} 
        }
    }
    I got incompatabile type at line 26 which is
    Java Code:
    IdolFinalist temp = v.get(i);

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I see it now. You are using a raw Vector, which always stores objects of type Object. You should either use a generic Vector<IdolFinalist> or you need to cast the object on your get() call. So either this:
    Java Code:
    		Vector<IdolFinalist> v = new Vector<IdolFinalist>();
    or this:
    Java Code:
    			IdolFinalist temp = (IdolFinalist)v.get(i);
    -Gary-

  7. #7
    xHoly is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks Gary!
    That was a great help, appreciate it. ^_^
    Added a Rep to you.

    Many Thanks!
    I'll keep in mind to use a generic Vector next time.

Similar Threads

  1. Vectors Problem
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 12:16 AM
  2. [SOLVED] Vectors
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 05-26-2009, 03:11 PM
  3. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  4. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 10:04 AM
  5. Vectors vs ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 11:29 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
  •