0

I have a utility class that checks to see if isNumeric or not by doing the following:

public static boolean isNumeric(String text) {
    try {
        Double.parseDouble(text);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

IntelliJ returns the following error:

Warning:(30, 20) Result of 'Double.parseDouble()' is ignored

How can I refactor my code to avoid this error message? I don't need the result of Double.parseDouble() to do anything and I don't want to also suppress the error message.

methuselah
  • 12,766
  • 47
  • 165
  • 315

2 Answers2

2

Use a @SuppressWarnings annotation on the method or the entire class:

@SuppressWarnings("UnusedReturnValue")

Or even better, don't reinvent the wheel and use the answers in this question instead!

Paul Benn
  • 1,911
  • 11
  • 26
2

With mine , i don't have any mistakes ,

 public boolean isNumber() {
    try {
        return !Double.isNaN(Double.parseDouble(this.answer));
    } catch (Exception e) {
        logger.error("Not a Number");
        return false;
    }
}

The method Double.isNaN(Your answer) makes the trick and is better than a SuppressWarning i think ;)