Results 1 to 10 of 10
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Can i use pregmatch in java code if statement?

    I remember i did some things with pregmatch in php, does it work the same way in java?

    If so, can someone help me get a correct 'pregmatch' for my textfields? In particular i need to check if:
    -only spaces entered in textfield
    -only numbers entered in textfield

    Also, can i insert this 'pregmatch' in a if statement, like this?
    Java Code:
    if(jTextField1.getText().equals(PREGMATCH) || jTextField1.getText().equals(PREGMATCH2) {
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Can i use pregmatch in java code if statement?

    Did you try it? What happened? Here's a good tutorial for how to use regular expressions in java:
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    There are methods in the String class (eg matches) that use regular expressions.

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can i use pregmatch in java code if statement?

    Thanks ill look into regular expressions

  4. #4
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can i use pregmatch in java code if statement?

    i found something like this:
    Java Code:
    String number = ...
    if (number.matches("^8\\d{9}$")) {
      // it's a number
    }
    would this work?:
    Java Code:
    if (jTextField1.matches("^8\\d{9}$")) {
    }
    also, how do i make it so it would be an opposite of 'matches'?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can i use pregmatch in java code if statement?

    Quote Originally Posted by iNko View Post
    i found something like this:
    Java Code:
    String number = ...
    if (number.matches("^8\\d{9}$")) {
      // it's a number
    }
    would this work?:
    Java Code:
    if (jTextField1.matches("^8\\d{9}$")) {
    }
    also, how do i make it so it would be an opposite of 'matches'?
    That's a very funny regular expression, i..e. it matches a number starting with an 8, followed by nine digits; is this what you had in mind? w.r.t. your last question: if 'x' is a boolean expression then !x is true whenever x is false and vice versa. Don't fall in the trap that you want to do everything with regular expressions ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can i use pregmatch in java code if statement?

    That expression is just a random expression that i found on google, i absolutely dont understand how they work (spent a couple of hours reading a 'simple' tutorial, and still couldnt understand anything..).
    Right now im just looking for an expression that would check if the textfield is a whole number, maximum 5 digits.

    Also for the 2nd part of my question, if i had this for example:
    Java Code:
    if (jTextField1.isEmpty(){
    }
    The opposite would be:
    Java Code:
    if (!jTextField1.isEmpty(){
    }
    Right? (dont know where to write '!').

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can i use pregmatch in java code if statement?

    Quote Originally Posted by iNko View Post
    That expression is just a random expression that i found on google, i absolutely dont understand how they work (spent a couple of hours reading a 'simple' tutorial, and still couldnt understand anything..).
    Right now im just looking for an expression that would check if the textfield is a whole number, maximum 5 digits.

    Also for the 2nd part of my question, if i had this for example:
    Java Code:
    if (jTextField1.isEmpty(){
    }
    The opposite would be:
    Java Code:
    if (!jTextField1.isEmpty(){
    }
    Right? (dont know where to write '!').
    That would be right if jTextField1 would be a String; the name suggests otherwise so I guess it should be jTextField1.getText().isEmpty() or (to check if the String is not empty): !jTextField1.getText().isEmpty(). A String that contains at most five digits only, matches the regular expression "\\d{,5}"

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Can i use pregmatch in java code if statement?

    Quote Originally Posted by iNko View Post
    Also for the 2nd part of my question, if i had this for example:
    Java Code:
    if (jTextField1.isEmpty(){
    }
    The opposite would be:
    Java Code:
    if (!jTextField1.isEmpty(){
    }
    Right? (dont know where to write '!').
    What happened when you tried it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can i use pregmatch in java code if statement?

    Quote Originally Posted by JosAH View Post
    That would be right if jTextField1 would be a String; the name suggests otherwise so I guess it should be jTextField1.getText().isEmpty() or (to check if the String is not empty): !jTextField1.getText().isEmpty(). A String that contains at most five digits only, matches the regular expression "\\d{,5}"

    kind regards,

    Jos
    Thank you, this worked for me:
    Java Code:
    if ( !jTextField.getText().matches("^\\d{1,5}$") ){
    }

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can i use pregmatch in java code if statement?

    Quote Originally Posted by iNko View Post
    Thank you, this worked for me:
    Java Code:
    if ( !jTextField.getText().matches("^\\d{1,5}$") ){
    }
    Note that you don't need the ^...$ because the matches( ... ) method tries to match the entire String, i.e. "\\d{1,5}" is enough.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. Replies: 1
    Last Post: 05-17-2011, 09:43 AM
  4. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 03:09 PM
  5. Code too large for try statement
    By vravindran in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 01-09-2009, 11:49 AM

Posting Permissions

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