tags:

views:

55

answers:

3

OK so for background I've only been using Java for a little more than a week and I am making a basic GUI "Recipe Book" application just to test out some of the techniques I've learned. Currently I'm working on the "Add New Recipe" page that allows the user to make a new recipe by adding ingredients one at a time. The ingredients have 3 attributes: Name, amount, and unit (like cups, oz, etc) that come from two text fields and a combo box respectively. The 3 values are stores as strings to a String array that represents the ingredient, then the arrays are stored in a vector that holds all of the ingredients for one recipe. When an ingredient is added, the user first types in the 3 necessary values into empty boxes/combo box dropdown and then clicks "Add Ingredient". Here is my code for when the button is clicked:

public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == addIngredientButton) {
        //make sure 3 ingredient fields are not blank
        if (!ingredientNameField.getText().equals("") && !ingredientAmountField.getText().equals("") && !ingredientUnitDropdown.getSelectedItem().equals("")) {
            tempIngredientsArray[0] = ingredientNameField.getText(); //set ingredient name to first position in array
            tempIngredientsArray[1] = ingredientAmountField.getText(); //set ingredient amount to second position in array
            tempIngredientsArray[2] = (String) ingredientUnitDropdown.getSelectedItem(); //set ingredient unit to third position in array
            int ingredientsVectorSize = tempRecipe.ingredientsVector.size();
            tempRecipe.ingredientsVector.add(this.tempIngredientsArray);  //why would it matter if this and previous statement were switched
            for (int k = 0; k < ingredientsVectorSize + 1; k++ ) {
                liveIngredientsListArea.append("\n" + tempRecipe.makeIngredientSentence(k));
                System.out.println(Arrays.toString(tempIngredientsArray));  //shows that tempIngredientsArray gets messed up each time
                }
            }

Now here's the really strange problem I've been having. The first time the user adds an ingredient everything works fine. The second time, the String[] for that ingredient seems to be duplicated, and the third time it's triplicated. Aka the first time it runs the System.out.println might return "{Sugar, 3, Cups}" and the second time it will return "{Flour, 2, Oz.} {Flour, 2, Oz.}" etc. What's causing this strange problem? All help greatly appreciated, let me know if you need any clarification.

A: 

Something I noticed:

tempRecipe.ingredientsVector.add(this.tempIngredientsArray);

That's adding tempIngredientsArray to your vector. The array, just like that. There's no copying going on or anything. The array, a single object, is added to the end of the vector. If you now put different values into the array, then it will contain different values. If you add the new, changed array to the vector, you will have the same array in the vector twice, with the most recent values, because... it's the same array.


EDIT:

My suggestion on creating the array inside the if:

if (!ingredientNameField.getText().equals("") ... {
    String[] tempIngredientsArray = new String[3];
    tempIngredientsArray[0] = ingredientNameField.getText();
        ...

... and you'll have to remove the declaration of tempIngredientsArray wherever it is now, of course.

Carl Smotricz
I realized upon fixing other parts of my code that this is the problem. How do I avoid this? You were saying something about copying?
hatboysam
I think that problem would be solved if you instantiated a new temp array right inside the `if` rather than re-using one you made beforehand.
Carl Smotricz
How do I do this? Do you mean if the field temp array were inside the If as a local variable or something? Sorry, this is my first week in Java.
hatboysam
Updated my post, hope this helps you.
Carl Smotricz
+1  A: 

First question in your comments:

int ingredientsVectorSize = tempRecipe.ingredientsVector.size();
tempRecipe.ingredientsVector.add(this.tempIngredientsArray); 
//why would it matter if this and previous statement were switched

Because in the first line you're getting the size of a Vector, then adding an element to it in the next. If you do that in the opposite order the size will be greater by 1.

To answer your main question, you're appending ingredients in a loop.

for (int k = 0; k < ingredientsVectorSize + 1; k++ ) {
    liveIngredientsListArea.append("\n" + tempRecipe.makeIngredientSentence(k));
    System.out.println(Arrays.toString(tempIngredientsArray)); 
    //shows that tempIngredientsArray gets messed up each time
}

Each ingredient is going to be added as many times as there are elements in ingredientsVector (plus one). So when you have an empty vector, the first ingredient is added once, the second ingredient added twice, and so on.

Just append each ingredient without looping and you should be fine.

Bill the Lizard
Aw, beat me to it as I was typing! =]
Xavier Ho
+1  A: 

Each time you do this

int ingredientsVectorSize = tempRecipe.ingredientsVector.size();
tempRecipe.ingredientsVector.add(this.tempIngredientsArray);

The size of ingredientsVector grows by one. The order matters because the size() would return differently.

Then,

for (int k = 0; k < ingredientsVectorSize + 1; k++ ) {
    liveIngredientsListArea.append("\n" + tempRecipe.makeIngredientSentence(k));

You appended the next item ingredientsVectorSize times. That's where the duplicates are coming from.

Simply remove the loop, append one item each time, and you'll be fine.

Xavier Ho