This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To fix an ArrayList API problem....

 
S. S. Diaz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!...I have an ArrayList problem(or defect) in Java because everytime this code snippet:

numReg.add(a);

inside the inner class runs, the object "a"(a double variable) won't add to the ArrayList. Is there anything that I can do to solve the problem? Here are my code fragments that is supposed to function well for my calculator project:

class subtractNum implements ActionListener { // an inner class for
// the subtraction.

public void actionPerformed(ActionEvent event){
if(!(numReg.contains(a))){
a = Double.parseDouble(field.getText());
numReg.add(a);
field.setText("0");
op = "-";
} else {
if (op == "-") {
a = a - Double.parseDouble(field.getText());
} else {
if (op == "+"){
a = a + Double.parseDouble(field.getText());
}
if (op == "*"){
a = a * Double.parseDouble(field.getText());
}
if (op == "/"){
a = a / Double.parseDouble(field.getText());
}
}
field.setText(a + "");
op = "-";
b = Double.parseDouble(field.getText());

}
new NumButtonPackage();
numReg.remove(b);
}
}

Thankz in advance!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is a JSP forum, post to the beginner forum next time

Why is "a" removed ? Maybe this could be a hint :

numReg.add(a);
field.setText(a + "");
b = Double.parseDouble(field.getText());
numReg.remove(b);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General (Beginner)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Are you aware of the fact that the value of "a" changes between the check in the if and the call to add?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic