Lee Chant
Lee Chant

Reputation: 103

Update value in Java ArrayList

I wasn't too sure what to title this question, apologies in advance. I currently have a value of say 50 stored in the BidderArray. I want to be able to increase that 50 by any given number entered into a text field.

So say I want to add 10 to the existing 50, it will return 60. Currently when I add the 10 the 50 is replaced by 10 instead of adding the two together. I understand why my code is doing this but haven't been able to find any tutorials or hints on what I should be doing instead.

Here is the code:

package abc;

import java.awt.*;


public class Funds extends javax.swing.JFrame {
    int i = 0;

    Bidder bidbal = new Bidder();
    /** Creates new form Funds */
    public Funds() {
        initComponents();
    }

    private void addActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        int f = 0;
        boolean validEntries = true;

        try{
            f = Integer.parseInt(amount.getText());
            Bidder.BidderArray.get(i).setRegFee(f);
        } catch (Exception error) {
            validEntries = false;
            amount.setBackground(Color.red);
        }
        if (validEntries) {
            Bidder.exportBidder();
            Home home = new Home();
            home.setVisible(true);
            this.dispose();
        }
    }
}

Upvotes: 0

Views: 310

Answers (4)

clcto
clcto

Reputation: 9648

You have to get the current fee, add the value, and then set the fee:

f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).setRegFee( f + Bidder.BidderArray.get(i).getRegFee() );

Or you could add a new method the Bidder class that adds to the fee:

class Bidder
{
    //...
    public void addToRegFee( int amount )
    {
        this.regFee += amount;
    }
}

f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).addToRegFee( f );

Upvotes: 1

jrowe08
jrowe08

Reputation: 391

You must add it to your old value:

  //Add old to new (Unless you have a different method set for get like
  //getRegFee (Not sure how bidder is implemented))
  Bidder.BidderArray.get(i).setRegFee(Bidder.BidderArray.get(i) + f);

Upvotes: 1

Santa
Santa

Reputation: 11547

    f = Integer.parseInt(amount.getText());
    Bidder.BidderArray.get(i).setRegFee(f);

Here, it seems like you're getting the user's input (f), and just set the array's element value to it. What it sounds like you want to do is to take that input (f), and the array's element existing value, combine (read: add) them, before setting the element's value to that new combined value.

In pseudo-code, this is what you're doing:

f := get user's input
setValue(f)

What you need to do:

f := get user's input
g := get current value
setValue(f + g)

Upvotes: 1

user177800
user177800

Reputation:

You aren't actually adding anything

Bidder.BidderArray.get(i).setRegFee(f);

is apparently just setting something to f, you have to get the current value, add to it, and then put it back. But this is just a guess as we don't have enough actual code to know what you are doing wrong.

Upvotes: 3

Related Questions