[Solved] String parsing – Java [closed]


Note: if you want to really do it right, you can implement a subclass of JTextField that doesn’t even let the user enter characters that are invalid floating-point numbers:

public class FloatField extends JTextField {

    public FloatField(int cols) {
        super(cols)
    } 

    protected Document createDefaultModel() { 
        return new FloatDocument();
    } 

    static class FloatDocument extends PlainDocument {

        public void insertString(int offs, String str, AttributeSet a)
                          throws BadLocationException { 
            if( str == null ) 
                return;

            // Reject any string with invalid float characters.
            // TODO: this could be more sophisticated
            int len = str.length();
            for (int i = 0; i < len; ++i) {
                char c = str.charAt(i);
                if (c != '.' && c != '-' && !Character.isDigit(c))
                    return;
            }
            super.insertString(offs, str, a);
        } 
    } 
} 

Note: this is an imperfect implementation for illustration purposes only, there are still many invalid floating-point numbers that this would allow to leak through.

solved String parsing – Java [closed]