Hello guys. I'm having a bit of difficulty with adding totals. I know how it's suppose to work but I'm not seeing what I'm doing wrong. If I want to add my total each time I hit the total button how can I do it. When I try to add it, it never works but lets say I do total = total +10;, it will do it only once no matter how many times I press the total button. This is an example of the code I have for now. So basically with this code, anytime I hit the total button it should keep adding 5 to the total. So the first time it will be 5, second time 10 and so forth. Thanks in advance!

total = 5;
newtotal = 0.0;

public void actionPerformed(ActionEvent e)
{
	String arg = e.getActionCommand();



    if(arg == "Total")
   {
      total = total + newtotal;
   }
}

Recommended Answers

All 4 Replies

Um, you can 0 to a value all day and it won't change...

total = 5;
newtotal = 0.0;

I tried newtotal; instead of newtotal = 0; also and it didn't work.

I would imagine that you are either resetting total or newtotal is not being captured like you think it is, because there is nothing wrong with that operation (except that you could use total += newtotal; instead).

I found out what I had to do. It's lame when you find out you make the simplest of mistakes. I needed to add an extra line newtotal = total;. Then everytime i pick the total button it adds the old total to the new total. My new code with it implemented is below, incase others may have the same question in the future. Thanks again Ezz.

total = 5;
newtotal = 0.0;

public void actionPerformed(ActionEvent e)
{
	String arg = e.getActionCommand();



    if(arg == "Total")
   {
      total = total + newtotal;
      newtotal = total; 
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.