Results 1 to 3 of 3
  1. #1
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Regular Expression issue and setName() method issue

    "The name must be of non-zero length and consist of only letters and at most one blank, which cannot be the first or last character."

    1. It requires the string to be at least 3 characters long. But "X" and "XX" meet the
    API specifications.
    2. It requires that the string contain exactly one whitespace character.
    So
    a. it rules out any strings without whitespace characters, like "XXX"
    b. it wants "whitespace" instead of a single "blank", so it accepts
    "XX\tXX" when it shouldn't.


    all to setName("X") fails: java.lang.IllegalArgumentException

    Call to setName("XX") fails: java.lang.IllegalArgumentException

    Call to setName("XXX") fails: java.lang.IllegalArgumentException

    Call to setName("a") fails: java.lang.IllegalArgumentException

    Call to setName("XXX XXX") should throw IllegalArgumentException, but does not.

    After call to setName("XXX XXX"), call to getName() returns "XXX XXX" instead of
    "unknown name".


    My Code ~~

    public String getName()
    {
    this.name = name;
    return (this.name);
    }
    public void setName(String name) throws IllegalArgumentException
    {
    String regex = "[A-Za-z]+(\\s[A-Za-z]+)";
    if (!name.isEmpty() && name.matches(regex))
    {
    this.name = name;
    }
    else
    {
    throw new IllegalArgumentException();
    }
    }

    Also I have a problem when the if statement is false I need it to automatically to switch to "unknown name" but when I do It still turns out as an exception. Theres something wrong with my regex but I don't know what.

    edit: regex = "[A-Za-z]+(\\s(^\\t[A-Za-z]?))" it's not right, but i know ^\\t will remove tab but don't know where to put it.
    Last edited by geforce; 01-30-2012 at 03:53 AM.

  2. #2
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Regular Expression issue and setName() method issue

    Bump

  3. #3
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Regular Expression issue and setName() method issue

    .... No one knows this?

Similar Threads

  1. Help with an input.Line issue in a method I've created
    By The_Steak in forum New To Java
    Replies: 3
    Last Post: 11-06-2011, 04:42 AM
  2. logical accept() method issue
    By senca in forum Networking
    Replies: 6
    Last Post: 11-01-2011, 10:34 PM
  3. Bubblesort Issue within Method
    By burrish in forum New To Java
    Replies: 9
    Last Post: 09-27-2011, 03:39 AM
  4. Issue in method anonymous class in GWT
    By ankit01 in forum GWT
    Replies: 0
    Last Post: 05-16-2011, 12:25 PM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 01:25 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
  •