#Require a specific format for a String in the constructor???
14 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @brisk fable! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Regular expressions probably
That's the easiest way
import java.util.regex.*;
public class MyClass {
private static final Pattern PATTERN = Pattern.compile("[A-Z]\\d");
private final String myString;
public MyClass(String myString) {
if (!PATTERN.matcher(myString).matches()) {
throw new IllegalArgumentException("Invalid string: " + myString);
}
this.myString = myString;
}
public String getMyString() {
return myString;
}
}
This is what it would look like
Give or take
That's a static definition of a regular expression pattern
So you can reuse it every time
One uppercase letter from A to Z, followed by one ASCII digit from 0 to 9
It imposes exactly two chars, a letter and a digit
you could also use myString.matches() directly
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.