Help with string.matches() in java!

  • Comp Sci
  • Thread starter kirkulator
  • Start date
  • #1
33
0
I have input from the user which is the last name, sent to be set as the last name for my object in my setLasttName method.

public void setLastName(String lName) throws IllegalArgumentException {
if (lName.matches("[a-zA-z'-]")) {
this.lastName = lName; }
else {
throw new IllegalArgumentException("Invalid Input: name can"
+ "only consist of letters, hyphens, or apostrophes. ");
}

now, i am only aloud to have a-z, A-Z, hyphens, and apostrophes in the name, else i throw an exception. I have run this with putting '++' and other illegal input for lastname and it WILL NOT CATCH the exception, it continues with the program. what am i doing wrong? i have searched online forever trying to find a simple way to do this. is there an easier way that will work? Thanks so much! Last project of the quarter ^.^
-Amanda
 

Answers and Replies

  • #2
34,687
6,394
Use [ code ] and [ /code ] tags around your code (without the spaces) to preserve indentation. I have done this for you.
I have input from the user which is the last name, sent to be set as the last name for my object in my setLasttName method.
Code:
public void setLastName(String lName) throws IllegalArgumentException {
        if (lName.matches("[a-zA-z'-]")) {
        this.lastName = lName; }
        else {
            throw new IllegalArgumentException("Invalid Input: name can"
                    + "only consist of letters, hyphens, or apostrophes. ");
        }
now, i am only aloud to have a-z, A-Z, hyphens, and apostrophes in the name, else i throw an exception. I have run this with putting '++' and other illegal input for lastname and it WILL NOT CATCH the exception, it continues with the program. what am i doing wrong? i have searched online forever trying to find a simple way to do this. is there an easier way that will work? Thanks so much! Last project of the quarter ^.^
-Amanda
I think it might be the ' character that is causing problems. See if this makes a difference: ["a-zA-Z\'-]"
 
  • #3
Filip Larsen
Gold Member
1,347
263
Some comments:
  • Your character ranges include the range A-z. You probably meant A-Z (capital z).
  • The matches method tries to match the whole string to the pattern so you would probably want to use a pattern like "[a-zA-Z'-]+" where the last + means "one or more" of the preceding pattern.
  • You say that you cannot get your code to throw IllegalArgumentException? If so, perhaps you are blind-catching the exception at some point. Check with a debugger or with debug logging that you enter the second branch of the test, or write a small unit test that calls your setter directly and checks for IllegalArgumentException.
 
  • #4
33
0
Thanks so much guys! The "+" at the end is what i needed. I'm not very familiar with the regex stuff. It seems every time i have a problem with code working its a dumb overlooking mistake. Also, thanks for the heads up on indentation! I apologize, I've never had to put code in this forum before. : )
 

Related Threads on Help with string.matches() in java!

Replies
2
Views
1K
Replies
2
Views
3K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
0
Views
4K
  • Last Post
Replies
5
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
3
Views
2K
Replies
2
Views
1K
Top