Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NumberFormatException nfe meaning in JAVA

I was making a program where the input must be an integer. I was trying to validate the user input, so I googled up a thing called the try/catch method.

The validation function looks like this:

public static boolean isNumeric(String strNum) {
        try
        {
            int i = Integer.parseInt(strNum);
        } catch (NumberFormatException nfe)
        {
            return true;
        }
        return false;
    }

I was trying to find more information about this line of code catch (NumberFormatException nfe), more precisely the meaning of NumberFormatException nfe this thing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Question: What exactly is NumberFormatException nfe? And how does it work?

>Solution :

Basically what’s happening here is the following:

  • You take the string strNum and give it to Integer.parseInt(), which is a function that will try to parse that String into an Integer.
  • If the method parseInt() doesn’t manage to parse your strNum (because it’s not an integer, for example because it is blabla or 2.35), then it will throw an exception of type NumberFormatException that is named nfe (it’s just a variable name in your code snippet).
  • If that doesn’t happen, you return true (meaning yes, your strNum is numeric). But if that happens, then you return false (meaning you didn’t manage to parse it as an Integer and so it’s not numeric).

A couple of things that may help you:

  1. The thing called try/catch method is called try/catch/finally block. The code you write inside the try will be attempted. If an exception is thrown while executing the code, it will be caught in the catch block (if declared) so that you can try to make it up. The finally block (which is optional, and in fact you’re not using it) will be executed no matter what happens, either you execute successfully or not. You should read more about how exceptions are handled in Java and what they are for, I’d suggest you here.
  2. Your function seems to guess whether a string is numeric or not. However, you’re using Integer.parseInt(). Be aware that the string 2.34, which is numeric, will be recognized as not numeric because 2.34 is a Double and not an Integer. So you may want to use Double.parseDouble() instead, in order to handle the cases where you receive a decimal digit (and it will work also for integer numbers)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading