![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 | |
|
Newbie
Join Date: Mar 2006
Posts: 4
Rep Power: 0
![]() |
Hi, I'm trying to code a calculator by using this following code:
**begning part removed for length reasons** one.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"1");
one.setPreferredSize(new Dimension(x, y));
}
});
two.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"2");
two.setPreferredSize(new Dimension(x, y));
}
});
three.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"3");
three.setPreferredSize(new Dimension(x, y));
}
});
four.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"4");
four.setPreferredSize(new Dimension(x, y));
}
});
five.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"5");
five.setPreferredSize(new Dimension(x, y));
}
});
six.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"6");
six.setPreferredSize(new Dimension(x, y));
}
});
seven.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"7");
seven.setPreferredSize(new Dimension(x, y));
}
});
eight.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"8");
eight.setPreferredSize(new Dimension(x, y));
}
});
nine.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"9");
nine.setPreferredSize(new Dimension(x, y));
}
});
zero.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText(aTextField1.getText()+"0");
zero.setPreferredSize(new Dimension(x, y));
}
});
aButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (add.equals(add))
{
String a = new String (aTextField1.getText());
aTextField1.setText("")
;}
}
});
aButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (minus.equals(minus))
{
String a = new String (aTextField1.getText());
aTextField1.setText("")
;}
}
});
aButton3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (multiply.equals(multiply))
{
String a = new String (aTextField1.getText());
aTextField1.setText("")
;}
}
});
aButton4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (divide.equals(divide))
{
String a = new String (aTextField1.getText());
aTextField1.setText("")
;}
}
});
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText("");
zero.setPreferredSize(new Dimension(x, y));
}
});
equals.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
if (add.equals(add))
{
String b = new String (aTextField1.getText() + a);
aTextField1.setText(b);
}
}
});
}
}Basically, I'm trying to get a user to input a series of numbers by keypad. Then when an operator button is pressed, the number entered is saved, and then the text field is cleared. But I have a problem, for some reason the String a = new String (aTextField1.getText()); doesn't seem to be called when i try to run the String b = new String (aTextField1.getText() + a);. Here's what i get: Quote:
To make it a little clearer, I'll describe in steps my main purpose -User enters (for example)"123" into the text field -User clicks an operator, this then takes the number in the text field, stores it as a string, and then clears the area -User enters (for example)"123" into the blank area -User hits the equals button, which the if statement will check what operator has been pressed, and then adds the two strings together and outputs the result. I'm a I on the right track at all? If not, please help. I've been racking my brain all day. Thanks to all those that can help.. |
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
a is not defined in the same scope as the statement that uses it. To use a, you must define it in the same function, or globally in the class.
|
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Mar 2006
Posts: 4
Rep Power: 0
![]() |
Quote:
Could you suggest a way I can define it in the same function? |
|
|
|
|
|
|
#4 | ||
|
Expert Programmer
|
Quote:
Quote:
1. if (add.equals(add))
2. {
3. String a = new String (aTextField1.getText());
4. aTextField1.setText("");
5. }* Actually, the variable "a" no longer references its previous value, in this case a String object, which is deleted when garbage collection occurs at some point in the future. |
||
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 4
Rep Power: 0
![]() |
Hi, I sorted out all the problems above thanks to the thread, but I have come up with a new problem. Basically, on the equals button I'm trying to tell it to either add, multiply, divide or minus.
So say if the user presses multiply the clocker will then equal 3..So when the equals button is pressed, it will look through the if statements till it gets to 3 and then do the required sum **at the very top of my code* int clocker = 0; aButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstInput = Integer.parseInt(aTextField1.getText());
clocker = 1;
aTextField1.setText("")
;}
});
aButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstInput = Integer.parseInt(aTextField1.getText());
clocker = 2;
aTextField1.setText("")
;}
});
aButton3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstInput = Integer.parseInt(aTextField1.getText());
clocker = 3;
aTextField1.setText("")
;}
});
aButton4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstInput = Integer.parseInt(aTextField1.getText());
clocker = 4;
aTextField1.setText("")
;}
});
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
aTextField1.setText("");
zero.setPreferredSize(new Dimension(x, y));
}
});
equals.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
secondInput = Integer.parseInt(aTextField1.getText());
if (clocker == 1)
{
sum = firstInput + secondInput;
String hi = new String (firstInput + "+" + secondInput + "=" + sum);
aTextField1.setText(hi);
}
if (clocker == 2);
{
sum = firstInput - secondInput;
String hi = new String (firstInput + "+" + secondInput + "=" + sum);
aTextField1.setText(hi);
}
}
});
}
}But I keep on getting the folowing error of sum.. ![]() I know what a boolean is, but I thought that it didn't need to be used here. If it does, could anyone point me out on how to do it depending what operator buttin was pressed Thanks Last edited by Java-Head; Apr 28th, 2006 at 8:32 AM. |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
if (clocker == 2);
{
sum = firstInput - secondInput;
String hi = new String (firstInput + "+" + secondInput + "=" + sum);
aTextField1.setText(hi);
} |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I guess that would do, i'm not very familiar with java.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|