|
-
August 18th, 2010, 12:18 PM
#1
What am I doing wrong with this Vector program?
I am learning Java on my own and one of the questions at the end of a chapter called 'Data Structures' states:
Create an application that uses a vector as a shopping cart that holds 'Fruit' objects. Each 'Fruit' object should have a name, quantity, and price.
Here is what I did but am getting 13 ERRORS mainly concentrating on my 2 while statements:
import java.util.*;
public class ShoppingCart {
public ShoppingCart() {
}
public static void main(String[] arguments) {
Vector<Fruit> cart = new Vector<Fruit>();
String[] fruitNames = { "Apple", "Orange", "Banana", "Grapes", "Pear" };
int[] fruitQuantity = { 3, 5, 2, 2, 4 };
float[] fruitPrices = { 2.49F, 1.75F, 1.25F, 3.99F, 2.25F };
try {
while(int i = 0; i < fruitNames.length; i++) {
cart.add(new Fruit(fruitNames[i], fruitQuantity[i], fruitPrices[i]));
} catch (NumberFormatException nfe) {
System.out.println("Error: " + getMessage());
}
while(int j = 0; j < cart.length; j++) {
System.out.println("Fruit: " + (String)cart.get(i).name);
System.out.println("Quantity: " + (String)cart.get(i).quantity);
System.out.println("Price: " + (String)cart.get(i).price + "\n");
}
}
}
class Fruit {
String name;
int quantity;
float price;
Fruit(String inName, int inQuantity, float inPrice) {
name = inName;
quantity = inQuantity;
price = (float)(inPrice * quantity);
}
}
Please advise. I looked at the answer for the question but don't see much of a difference other than they are individually creating 'Fruit' objects instead of using an array for them. Thanks so much for any help!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|