0

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();
        }
    }
}
3
  • I don't know what a Bidder is, but one possible reasoning for your problem is that when you add the 10 elements to your ArrayList, you are actually changing the reference to a new ArrayList, and then adding the 10 elements to that new List. Commented Mar 24, 2014 at 19:42
  • @NESPowerGlove Yes I understand that is what I am currently doing, how would I add 10 to the existing 50 in the Bidder array? The purpose is to increase the amount of tokens a user has. The token total displays in the Home class Commented Mar 24, 2014 at 19:45
  • 1
    More questions and references to stuff that you only know! How is anyone supposed to answer questions about code you don't post? Commented Mar 24, 2014 at 19:46

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 );

1 Comment

Thank you @clcto the first method you suggested has worked exactly as I intended. Sorry for being vague to those of you that picked me up on that. I'm new to Java and I'm not entirely sure how much code to post
1
    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)

Comments

1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.