0

So i have these lines:

try {
   extra = Double.parseDouble(igu.txfExtra.getText());
} catch (NumberFormatException e1) {
   System.out.print("Error");               
}

It collects a double from a JTextField called txfExtra.

How can I say in the System.out.print if the error was made by introducing letters in the label for example? I mean, if the error was due to the "extra" collecting a String, show that the error was due to a string.

Also an extra question, how can i make "extra" to take values with BOTH "." and "," because due to localization it either takes for example "10.923" or either "10,923" i want it to accept both types of format when parsing the double.

4
  • You will need something other than Double.parseDouble() for your first requirement. As to the second, NumberFormat may help. Also, do you accept numbers using the scientific notation? Commented Feb 15, 2014 at 13:34
  • each exception type is quite restrictive. Inside the catch you can check variables manually with some ad hoc code. Commented Feb 15, 2014 at 13:34
  • Have you tried calling e1.printStackTrace();? Commented Feb 15, 2014 at 13:38
  • You could always use regular expressions to check for the existence of any letters prior to doing the parseDouble. Which is what fge was referring to with the NUMPATTERN. Commented Feb 15, 2014 at 21:19

1 Answer 1

1

In order to satisfy both requirements, you would need something else than Double.parseDouble(). For one, it will use the current locale; also, its error message won't be as detailed as you want to be.

A solution would be to go through a regex to parse the input string, and only if the regex passes, parse using a NumberFormat:

private static final Pattern NUMPATTERN = Pattern.compile("\\d+([.,])\\d+");

// ...

// Supposes a custom MyException class
public double doParseDouble(final String input)
    throws MyException
{
    final Matcher m = NUMPATTERN.matcher(input);
    if (!m.matches())
        throw new MyException("Non number characters in input");

    final String separator = m.group(1);
    final Locale locale = ".".equals(separator)
        ? Locale.US : Locale.FRENCH;

    final NumberFormat fmt = NumberFormat.getInstance(locale);

    try {
        return fmt.parse(input).doubleValue();
    } catch (NumberFormatException e) {
        throw new MyException(e);
    }
}

This is only sample code and lacks a LOT of features:

  • restricts to only a subset of valid doubles (.1 will not be accepted for instance; or 1.4e-3);
  • does not deal with overflows/underflows;
  • others.
Sign up to request clarification or add additional context in comments.

1 Comment

private static final Pattern NUMPATTERN = "\\d+([.,])\\d+"; doesnt seem to work, is it for java 1.7 ? I get an error in eclipse saying "Change type of NUMPATTERN to string rofl

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.