textfield2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actionevent)
{
String input = textfield2.getText();
Output2.setText("Your age is " + (2017-input));
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actioneven)
{
String input = textfield2.getText();
Output2.setText("Your age is " + (2017 -input));
}
});
I am trying to get a number from the user (an integer) and subtract that number from 2017. It gives an error saying that it is a String and I can't subtract a number from a String. When I change String input
to int input
it gives errors again. It says that I can't use getText()
. I've tried few ways such as parseInt()
and it did not work. How can I fix this problem?
My Question is different from How to convert a String to an int in Java? because I looked at the answers there and they didn't really work with my code.