Results 1 to 6 of 6

Thread: Control numeric String in java

  1. #1
    Join Date
    Mar 2010
    Posts
    203

    Control numeric String in java

    Hello,
    How to control Java Strings which are numeric, i.e between 00.00 and 20.00 and that does not contain alphabetic or other character (eg # abc etc). Any help on this is hihgly appreciated. This because I want to know the basics of the java programming language from the scratch. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Control numeric String in java

    Hello,
    With a regular expression, it is almost done "just for that."
    Here is an example show the same
    Code:
    String ex = "([0-1][0-9]\\.[0-9][0-9])|(20\\.00)";
    If you know the regular expression in java then it would be better for understanding the concept behind this. If you need information on the expression in java, then you can take any java book and can do it quit easily. Best of Luck

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Control numeric String in java

    Hello,
    You can also do a KeyListener, when entering a character, if not a digit or period or an arrow to redirect or exhaust a character you do. You can also manage the event-related focus, then when the control loses focus, you test if the value is correct, if not you if it displays an appropriate error message and it gives you focus. Do you have knowledge about the swing concept in java? If not then I think just do the basic of it , they will be helpful for this.

  4. #4
    Join Date
    Dec 2009
    Posts
    213

    Re: Control numeric String in java

    Hello,
    Just have a look at the following.
    This is just an example and not perfect for the code
    Code:
    "([0-9]) | ([0-1][0-9]) | ([0-1][0-9]\\.[0-9]) | ([0-1][0-9]\\.[0-9][0-9]) |
    (\\.[0-9]) | (\\.[0-9][0-9]) |(20) | (20\\.00)
    I do not know that this is what you want, but still you can take it if you need and use in your code if it is helpful.

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Control numeric String in java

    Hello,
    It's okay (except that it 20.0 and perhaps other cases: I have not checked that your expression was exhaustive) The "\ \." means the character '.', if you forget the double \ \ in front, what is sought is not a point but "any character". If you need you can take a tutorial on the expression in java and how to use them in the code.

  6. #6
    Join Date
    Nov 2009
    Posts
    347

    Re: Control numeric String in java

    Hello,
    If you need you can take a look at the following code, see if it helps you.
    Code:
    double nt = 0;
    try{
        nt = Double.parseDouble(chnt.getText());
     
        if(nt <0 | | nt > 20){
            throw(new NumberFormatException());
        }
    }catch(NumberFormatException e){
        / / Display in your interface with an asterisk on the field notes and an info. stating the accepted values
    }

Similar Threads

  1. Definition of a String in Java?
    By cloud101 in forum Software Development
    Replies: 5
    Last Post: 28-07-2014, 10:14 AM
  2. The String Class in Java
    By blueprats in forum Guides & Tutorials
    Replies: 3
    Last Post: 12-03-2010, 02:44 PM
  3. Separating a string in java
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 04:27 AM
  4. Java String test
    By Aidan 12 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 02:22 PM
  5. How to sort numeric and string value in dataview
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 21-07-2009, 12:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,427,820.76127 seconds with 16 queries