How do I pull object information out of a vector.

I started in VB, and am now developing with java, so I'm finding small trip-ups which are to be expected.

Anyways, I'm trying to get an object element from an object in a vector.

I tried this, but it wouldn't return the "Name" query method:

System.out.println(myvector.get(0).Name.toString);

The object is a "Product" which I made the class for and everything. I even tried using typecast with no avail:

System.out.println((Product)myvector.get(0).Name.toString);

This is prolly one of the most basic things, yet I've found nothing on object attribute retrieval from a vector on google and wikipedia for that matter.

[663 byte] By [zensunnia] at [2007-11-26 15:53:10]
# 1
Try:System.out.println(((Product)myvector.get(0)).Name);You were trying to cast the String to a Product. Also, the toString() method is implied, you don't need to include it.
CaptainMorgan08a at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks a lot.That was pretty much a project-stopper for me.My hat's off to you :)
zensunnia at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 3
Since the brackets were left off that would imply that toString is a member of the Name object. ;)
floundera at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for the help, but I must just be slow or something...

Apparently I don't know how to create a function either and I don't want to create another thread to ask....

Now, I need a function to accept the Vector, manipulate it, and return a double.

Something like this:

public double getSumOfProductPrice(Vector productvector)

{

<manipulate productvector>;

return <result>;

}

I dunno, but everything I type gives me red lines.

Message was edited by:

zensunni

zensunnia at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 5
> Now, I need a function to accept the Vector,> manipulate it, and return a double.Alright. How do you want to manipulate it, and what do you want to return. Your question is very vague.
CaptainMorgan08a at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 6

Yea, it is..sorry

The Product class looks like this:

public class Product

{

private int mProductID;

private String mName;

private double mPrice;

private int mQuantity;

// Update Methods

public void ProdID(int id) {mProductID = id}

public void Quantity""

etc

etc

// Query methods

public int ProdID() {return this.mProductID;}

etc

etc

}

Basically, I want a function in my main class to accept a Vector filled with product objects and calculate a sum of all the products and return a double type value.

I'm fine with manipulating the vector, I just don't know how to state the function.

Message was edited by:

zensunni

zensunnia at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 7
declare sumloop over elements in vector {add current elements price to sum}return sum
floundera at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 8
I'm not quite sure what your criteria is for calculating the sum of a Product, but here's how you would do it. Iterate through the vector, adding each Products "value" to some variable that keeps the sum. Remember to cast properly!
CaptainMorgan08a at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...
# 9
I've figured it out.I was trying to put a function in the "static main", which, aparently you can't do. Thanks again for the help all.
zensunnia at 2007-7-8 22:13:36 > top of Java-index,Java Essentials,New To Java...