1

I was wondering how you would go about outputting which of the two text boxes is holding the NumberFormatException.

try
    {
        num1Convert = Integer.parseInt(num1Str);
        num2Convert = Integer.parseInt(num2Str);

        sumValue = num1Convert + num2Convert;
        sumLabel.setText(sumText + Integer.toString(sumValue));
    }
    catch(NumberFormatException nfe)
    {
        errorLabel.setText((HERE IS WHERE I NEED TO PUT CODE TO SAY WHICH TEXTFIELD IT IS" must be an integer");
        num1.requestFocus();


    }

my program compares two numbers, and then returns the value of the numbers added together, but I need to output which of the two textareas are throwing back the exception, but I don't know how to do this. I have wrote within the code where it is necessary to output it.

Luke
  • 37
  • 8
  • 1
    how about two seperate try catch blocks? Or is that too obvious a solution? – OH GOD SPIDERS Mar 08 '17 at 14:23
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Apr 10 '17 at 09:36

2 Answers2

2

How about this :

    try{
        num1Convert = Integer.parseInt(num1Str);
    }
    catch(NumberFormatException nfe) {
        System.out.println("Exception in num1");
    }
    try{
        num2Convert = Integer.parseInt(num2Str);
    } catch(NumberFormatException nfe) {
         System.out.println("Exception in num2");
    }

    //EDIT

    sumValue = num1Convert + num2Convert;
    sumLabel.setText(sumText + Integer.toString(sumValue));
Anand Undavia
  • 3,493
  • 5
  • 19
  • 33
  • But then how do I go about adding the two values as shown within the try catch i posted? – Luke Mar 08 '17 at 14:26
  • Edited the answer – Anand Undavia Mar 08 '17 at 14:28
  • This works well but it sets the calculation value to just the number which is an integer so for example if num1 is 5 and num2 is 5t it will set the sumvalue to 5, rather than waiting until the number exception has been fixed before calculating a value – Luke Mar 08 '17 at 14:36
  • You can have a boolean. The boolean will false if any of the catch block is executed. If the boolean is false that means any of the textfield had string which can't be converted to number – Anand Undavia Mar 08 '17 at 14:39
1

Something like this should do:

String currentString = "";
try
    {
        currentString = num1Str;
        num1Convert = Integer.parseInt(num1Str);
        currentString = num2Str;
        num2Convert = Integer.parseInt(num2Str);

        sumValue = num1Convert + num2Convert;
        sumLabel.setText(sumText + Integer.toString(sumValue));
    }
    catch(NumberFormatException nfe)
    {
       // errorLabel.setText((HERE IS WHERE I NEED TO PUT CODE TO SAY WHICH TEXTFIELD IT IS" must be an integer");
       errorLabel.setText(currentString + " must be an integer");
        num1.requestFocus();

    }
ininprsr
  • 2,472
  • 2
  • 14
  • 9