CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2017
    Posts
    5

    RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    Hello all,

    First post having migrated to a new home hopefully

    I have a project here Im working on to consolidate my work, Im new to Java and my knowledge is still lacking.

    I am working on a simple database of invoices for my Nephew and Im having problems setting up a numeric filter on the 'create invoice' form. I have a long list of work related tasks which need to be filled in one by one if the work has been done. No prizes for guessing, each of these item are represented by a label and a JTextField holding the current value.
    At the bottom there is a total. When this screen is first displayed each of the TextFields has the value 0.00 in because its a new Invoice. I wanted each value in each textfield to show the following behaviour,
    As the person types in a new value, once the value looses focus, the value is examined and if there is no decimal point and 2 digits after then it adds them, if there is one it adds a '0' also if the string starts with a decimal point then the function adds a zero - so,

    12.1 becomes 12.10,
    .02 becomes 0.02
    .2 becomes 0.20

    So I thought this would work,

    Code:
    DocumentFilter numericFilter = new DocumentFilter(){   
    
            @Override
            public void insertString(DocumentFilter.FilterBypass fb, int offset,
                    String string, AttributeSet attr)
                    throws BadLocationException {
                fb.insertString(offset, string.replaceAll("[^\\d]", ""), attr);
            }
    
            @Override
            public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
                    String text, AttributeSet attrs)
                    throws BadLocationException {
    
                fb.replace(offset, length, text.replaceAll("[^\\d]", ""), attrs);
            }
        };
    ....and it does, sort of up to a point but it allows for more than one decimal point? and no adding of zeros ?

    So I've looked around for a regEx that will work and Im getting no where -

    I'm starting to think that maybe I could do this the old way in a set of If....then.... statements might be a better way.

    Anyway, over to this community, my name is Phil, Im a very old hand at programming but new to Object Orientated Java, so a little out of my depth at the moment> ;-)

  2. #2
    Join Date
    Jun 2017
    Posts
    5

    Re: RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    Im trying to edit the above and cannot,

    the code regex should read,

    .replaceAll("[^\\d\\.]", ""), attrs)

    ie I added the decimal point

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    Sorry, regular expressions have nothing to do with OOP or java. They are a mystery unto themselves.

    12.1 becomes 12.10,
    .02 becomes 0.02
    .2 becomes 0.20
    The formatting would be done with the Formatter class or the String class's format() method
    Last edited by Norm; June 24th, 2017 at 05:24 PM.
    Norm

  4. #4
    Join Date
    Jun 2017
    Posts
    5

    Re: RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    Hmmm I think I was getting there myself, I've chosen the wrong method even though this was adapted from something similar....
    Sometimes its just not knowing which is best...

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    Have you considered using JFormattedTextField instead of JTextField? You can forget about filtering and formatting. Have a look at the Java Tutorial (I wanted to post the link, but it errors every time I tried).

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: RegEx problems, trying to force decimal point and 2 digits on a numeric filter.

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured