1

I have to create a calculator application at school and the calculator is working fine, but now I have to create an exception that shows a user friendly message when the user enters a string instead of an integer value.

This is the code I have now with a JFrame and everything:

private void minActionPerformed(java.awt.event.ActionEvent evt) {

    String invoerEen = getalEen.getText();
    int invoerEenDef =  Integer.parseInt(invoerEen);
    String invoerTwee = getalTwee.getText();
    int invoerTweeDef = Integer.parseInt(invoerTwee);

    int resultaatDef = invoerEenDef - invoerTweeDef;
    resultaat.setText(Integer.toString(resultaatDef));
}

private void plusActionPerformed(java.awt.event.ActionEvent evt) {
    String invoerEen = getalEen.getText();
    int invoerEenDef =  Integer.parseInt(invoerEen);
    String invoerTwee = getalTwee.getText();
    int invoerTweeDef = Integer.parseInt(invoerTwee);

    int resultaatDef = invoerEenDef + invoerTweeDef;
    resultaat.setText(Integer.toString(resultaatDef));
}

private void keerActionPerformed(java.awt.event.ActionEvent evt) {
    String invoerEen = getalEen.getText();
    int invoerEenDef =  Integer.parseInt(invoerEen);
    String invoerTwee = getalTwee.getText();
    int invoerTweeDef = Integer.parseInt(invoerTwee);

    int resultaatDef = invoerEenDef * invoerTweeDef;
    resultaat.setText(Integer.toString(resultaatDef));
}

private void delenActionPerformed(java.awt.event.ActionEvent evt) {
    String invoerEen = getalEen.getText();
    int invoerEenDef =  Integer.parseInt(invoerEen);
    String invoerTwee = getalTwee.getText();
    int invoerTweeDef = Integer.parseInt(invoerTwee);

    int resultaatDef = invoerEenDef / invoerTweeDef;
    resultaat.setText(Integer.toString(resultaatDef));
}

This code contains all the events and the actions they perform (the first one is for subtraction etc...). I use Netbeans as my development environment and I've tried adding try and catch statements like so:

try {
        String invoerEen = getalEen.getText();
        int invoerEenDef =  Integer.parseInt(invoerEen);
        String invoerTwee = getalTwee.getText();
        int invoerTweeDef = Integer.parseInt(invoerTwee);

        int resultaatDef = invoerEenDef - invoerTweeDef;
        resultaat.setText(Integer.toString(resultaatDef));
    }
catch(/* what do I put here? */) {
        // what do I do here?
}

So my question is, how do I create an exception for when a user enters a value that's not the correct return type (required: Integer, found: String).

Doubles or other return types are not important at the moment, only string.

Thanks in advance!

0

3 Answers 3

2

To catch when there is a String entered (blank text counts too), use NumberFormatException:

catch (NumberFormatException n) {
     System.out.println("Please enter a number.");
}

To catch any error, use Exception:

catch (Exception e) {
     System.out.println("Error.");
}

You can combine these to catch different errors so you can handle them separately. For example:

try 
{
    String s = "hi";
    int i = Integer.parseInt(s); // will go into NumberFormatException n
    s = s.substring(-2); // will go into Exception e
}
catch (NumberFormatException n) {
    System.out.println("Please enter a number.");
}
catch (Exception e) {
    System.out.println("Unspecified error (not a NumberFormatException).");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use NumberFormatException. It's thrown when a method that converts a String to a number receives a String that it cannot be converted. like Integer.parseInt("sadfasd");

Comments

2

You can use NumberFormatException javaDoc or Exception e for generic exceptions and then print a message like: Invalid entry.

catch (NumberFormatException e) {
    System.out.println("Invalid entry, please enter a number");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.