Results 1 to 3 of 3
- 01-30-2012, 02:25 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
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.
- 01-30-2012, 03:52 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Regular Expression issue and setName() method issue
Bump
- 01-30-2012, 04:33 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Help with an input.Line issue in a method I've created
By The_Steak in forum New To JavaReplies: 3Last Post: 11-06-2011, 04:42 AM -
logical accept() method issue
By senca in forum NetworkingReplies: 6Last Post: 11-01-2011, 10:34 PM -
Bubblesort Issue within Method
By burrish in forum New To JavaReplies: 9Last Post: 09-27-2011, 03:39 AM -
Issue in method anonymous class in GWT
By ankit01 in forum GWTReplies: 0Last Post: 05-16-2011, 12:25 PM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 01:25 PM
Bookmarks