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!!
Re: What am I doing wrong with this Vector program?
If you want help with errors, please post the full text of the errors plus stack trace, if available. Java errors generally tell you exactly what the problem is and where.
I can't read your code clearly because it's unformatted (see my sig), but if you control your loop with variable 'j', you'' generally want to use that variable inside the loop, and when you output an exception message, you should call the getMessage() method on the exception object.
Tell me and I forget. Show me and I remember. Involve me and I understand...
Chinese proverb
Re: What am I doing wrong with this Vector program?
Oh, thanks for pointing out the missing exception object for the getMessage() function to reference. That was an oversight. Also, the use of the 'j' variable within it's corresponding loop was an oversight. I have made those two corrections. Here are the errors that I am getting:
ShoppingCart.java:13: '.class' expected
while(int i = 0; i < fruitNames.length; i++) {
- the 'i' in the initialization section is being pointed at
ShoppingCart.java:13: illegal start of expression
while(int i = 0; i < fruitNames.length; i++) {
- the '=' in the initialization section is being pointed at
ShoppingCart.java:13: ';' expected
while(int i = 0; i < fruitNames.length; i++) {
- the space after the '=' in the initialization section is being pointed at
ShoppingCart.java:13: > expected
while(int i = 0; i < fruitNames.length; i++) {
- the ';' at the end of the condition section is being pointed at
ShoppingCart.java:13: not a statement
while(int i = 0; i < fruitNames.length; i++) {
- the '<' in the condition section is being pointed at
ShoppingCart.java:13: ';' expected
while(int i = 0; i < fruitNames.length; i++) {
- the ')' is being pointed at
ShoppingCart.java:15: 'catch' without 'try'
} catch (NumberFormatException nfe) {
- the 'c' in the word 'catch' is being pointed at
ShoppingCart.java:15: ')' expected
} catch (NumberFormatException nfe) {
- the space between 'NumberFormatException' and 'nfe' is being pointed at
ShoppingCart.java:15: not a statement
} catch (NumberFormatException nfe) {
- the '(' is being pointed at
ShoppingCart.java:15: ';' expected
} catch (NumberFormatException nfe) {
- the ')' is being pointed at
ShoppingCart.java:38: reached end of file while parsing
}
- there is an arrow pointing to the right after this closing brace and that is what is being pointed at
There are also a repeat of errors that match exactly to the first 6 but just with line number 19 and referring to the while loop containing the j variable.
Also, I am new so I was not sure how to setup the code in the thread. I don't see where the '#' or code button is. Where do I go to look at your sig? Not sure what that is. Thanks!!
Re: What am I doing wrong with this Vector program?
Dear Madam: The most obvious problem are your loop statements. You are using the "for" construct but have placed "while" in the "for" statement. They should be like this:
for (int i = 0; i < fruitNames.length; i++) {
// loop code
}
You have a few brackets out of place. Carefully indent your code so you can keep track of bracket beginnings and endings. The best method of going about programming, is to write in small increments, compile, check errors, fix and continue. That way you won't get discouraged and overwhelmed by a huge number of error messages.
You don't need to do any casting in the println statements. The println method uses what is called method over-loading. It detects integers, doubles, floats and println automatically converts them to a string.
ex. System.out.println("Quantity: " + (String)cart.get(i).quantity);
Ex. System.out.println("Fruit: " + (String)cart.get(i).name);
You can use:
System.out.println("Fruit: " + cart.get(i).name);
System.out.println("Quantity: " + cart.get(i).quantity);
Your output looks like this when you remove the errors. Worry about this later. Instead of float use double, most decimal calculations are done with double. As you can see, the total price of the Apples is a bit weird.
Anyways, good luck and keep at it.
Fruit: Apple
Quantity: 3
Price: 7.4700003
Fruit: Orange
Quantity: 5
Price: 8.75
Fruit: Banana
Quantity: 2
Price: 2.5
Fruit: Grapes
Quantity: 2
Price: 7.98
Fruit: Pear
Quantity: 4
Price: 9.0
Re: What am I doing wrong with this Vector program?
goksanen and dlorde, THANK YOU SO MUCH!!! goksanen, your notes were dead on point and helped me to get my program running. In addition to the loops needing to be changed, the unnecessary casting and the extra brace messing things up, I also had to change the condition in the second loop to call the vector's size() method instead of a 'length' method that I was trying to call. Again, I truly appreciate your help in this and now I can move forward!!