Help with string.matches() in java

  • Context: Comp Sci 
  • Thread starter Thread starter kirkulator
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around the implementation of input validation for a last name in Java using the `String.matches()` method. Participants explore regular expressions and exception handling in the context of ensuring that user input conforms to specific character requirements.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Amanda describes an issue where her `setLastName` method does not throw an `IllegalArgumentException` for invalid input, despite her regex pattern intended to restrict characters to letters, hyphens, and apostrophes.
  • Some participants suggest that the regex pattern may be incorrect, pointing out that Amanda's character range includes 'A-z', which may lead to unintended matches.
  • Another suggestion is to modify the regex to include a '+' at the end, indicating that the pattern should match one or more valid characters.
  • Participants also raise the possibility that the exception might not be caught due to blind-catching elsewhere in the code, recommending the use of debugging techniques to verify the flow of execution.

Areas of Agreement / Disagreement

There is no consensus on the exact cause of the issue Amanda is facing, but participants generally agree on the need for a correct regex pattern and the importance of debugging to identify where the exception handling may be failing.

Contextual Notes

Participants note potential limitations in Amanda's regex pattern and the need for careful attention to character ranges. There is also an acknowledgment of the complexity of regex for those unfamiliar with it.

Who May Find This Useful

Readers interested in Java programming, particularly in input validation, exception handling, and regular expressions, may find this discussion beneficial.

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 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K