Question:
Java Prog. Question (Number Format Exception)?
WitheredGryphon
2012-05-02 20:09:17 UTC
if (e.getSource() == Calc.eqls) {
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
Three answers:
coachabower
2012-05-02 20:19:58 UTC
The one that gets add-1 needs a second parameter to end the substring, otherwise you take everything from the index you use to the end of the line.



so, subString(add-1,add) maybe add-1, i forget how the stopping param works, but one of the two will do it.



Good Luck, hope this helps :)



edit: Or you can use charAt() as an alternative.



Actually your best bet is subString(0,add) just in case its more than a single digit.
husoski
2012-05-02 21:01:20 UTC
If they are indeed numbers, then the two substrings you want are:



String left = curText.substring(0,add).trim();

String right = curText.substring(add+1).trim();



The .trim() calls are mainly for your use in reporting errors if you catch a NumberFormatExceptions.



Integer leftval=null, rightval=null;

try {

leftval = Integer.parseInt(left);

rightval = Integer.parseInt(right);

}

catch (NumberFormatException ex)

{

// leave one or both vars set to null

}

if (leftval == null) {

JOptionPane.showMessageDialog(null,

"Invalid operand: " + left,

"Error", JOptionPane.ERROR_MESSAGE);

}

else if (rightval == null) {

...same error message for (right) string

}

else

{

Calc.tf.setText("" + (leftval + rightval));

}



That uses ""+() as a shorthand for Integer.toString() or String.valueOf(). Make sure you put parentheses around a numeric expression so that the calculation is completely done before string conversion. For example, "" + 1 + 2 returns "12", while "" + (1 + 2) returns "3".



Edit: Those .trim() calls are more necessary than I first thought. Integer.parseInt() does not accept leading or trailing spaces.
anonymous
2016-12-16 00:52:16 UTC
The argument you ought to be passing in Integer.ParseInt(arg) in basic terms isn't good formed.learn for null arguments.learn if the argument has particular characters.placed a try around the code of line throwing the exception and upload a grab block.to earnings if the argument handed is criminal, attempt printing out the argument and checking.the completed outstanding.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...