Comp Sci Help with string.matches() in java

  • Thread starter Thread starter kirkulator
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The issue with the string.matches() method in the setLastName function arises from the regex pattern used, which should be corrected to "[a-zA-Z'-]+" to ensure it matches one or more valid characters. The original pattern mistakenly included the range A-z, which is incorrect, and the absence of the "+" quantifier meant it only checked for a single character. Additionally, if the exception is not being caught, it may be due to blind-catching elsewhere in the code, so debugging or unit testing is recommended to verify the flow. The discussion emphasizes the importance of proper regex syntax and debugging techniques in Java. Overall, refining the regex and checking for exception handling will resolve the issue.
kirkulator
Messages
32
Reaction score
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
 
Physics news on Phys.org
Use [ code ] and [ /code ] tags around your code (without the spaces) to preserve indentation. I have done this for you.
kirkulator said:
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\'-]"
 
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.
 
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. : )
 

Similar threads

Replies
12
Views
2K
Replies
2
Views
1K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top