Results 1 to 5 of 5
  1. #1
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default matching problem

    can someone please tell me what I am missing in this field cheker. I only want it to take numbers but it still takes letters

    Java Code:
             gpaField.setColumns(4);
             String gpcheck = gpaField.getText();
                 //float gpanum = Float.parseFloat(gpaField.getText());
                 if (gpcheck.length()<= 0 || gpcheck.length()>=4 || gpcheck.matches("^([0-9]{0,1}[.]{1}[0-9]{3})")){
                            JOptionPane.showMessageDialog(null, "Please enter correct GPA");
                            gpaField.setBackground(Color.yellow);
                          IllegalArgumentException exception = new IllegalArgumentException("Invalid input");
                          throw exception;
                        }
                 else {
                     gpaField.setBackground(new JTextField().getBackground());
                 }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your IF statement says:
    "If the length is less than zero, or the length is greater than four, or it is a number with 3 decimal places, then it is invalid."

    What you want it to say:
    "If the length is less than zero, or the length is greater than four, or it is NOT a number with 3 decimal places, then it is invalid."

  3. #3
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    so how do I say NOT I tried ! no luck and the Pattern (Java 2 Platform SE v1.4.2)
    doesn't really specify a NOT

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For NOT, use !. You've used ! in if statements before surely. And if it doesn't work, then we can't help you unless you post your attempt.

  5. #5
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    sorry I got it... the ! was in the wrong spot. seam to be having a P.I.C.N.I.C. today

Similar Threads

  1. Need help with matching game
    By soundymcsoundsound in forum New To Java
    Replies: 2
    Last Post: 11-02-2010, 11:09 PM
  2. Reflective constructor matching problem
    By baizba in forum Advanced Java
    Replies: 2
    Last Post: 09-28-2010, 09:31 PM
  3. Paranthesis Matching???
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 10-27-2009, 06:02 AM
  4. Help with signature matching
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:21 PM

Posting Permissions

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