Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 763929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:38:21+00:00 2026-05-14T16:38:21+00:00

OK so for background I’ve only been using Java for a little more than

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:38:21+00:00Added an answer on May 14, 2026 at 4:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 1) set A breakpoint in the first line of page_load… May 14, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer The closest you can get is a static field outside… May 14, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer Perhaps some old fashion fixed point math could help you… May 14, 2026 at 7:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.