WitheredGryphon
2012-05-02 20:09:17 UTC
curText = Calc.tf.getText();
if(curText.contains("*")) {
if(curText.contains("+")) {
int add = curText.indexOf("+");
Calc.tf.setText((curText.substring(add-1)+curText.substring(add+1)));
}
else if(curText.contains("-")) {
int subtr = curText.indexOf("-");
Calc.tf.setText((curText.substring(subtr-1)+curText.substring(subtr-1)));
}
}
else if(curText.contains("+")) {
int add = curText.indexOf("+");
int result = Integer.parseInt(curText.substring(add-1)) + Integer.parseInt(curText.substring(add+1));
String result2 = Integer.toString(result);
Calc.tf.setText(result2);
}
}
I'm receiving a number format exception at the below line. I'm trying to make it so that I can take the numbers preceding and succeeding the ' + ' operator, add them together, then reconvert them back to a string so I can output it to the textfield.
int result = Integer.parseInt(curText.substring(add-1)) + Integer.parseInt(curText.substring(add+1));
Variables:
tf = JTextField
Calc = Main Class
Rest are self defined.
Also, debugging it reports that the local variables are unavailable (I'm unsure if this is just a repercussion of a number format exception?)
Code unedited by yahoo is located at:
http://pastebin.com/5S4rZGnp