import java.util.Scanner;
public class VerifySerialBayneHarris {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String text;
String cha;
System.out.println("Enter a Serial number: ");
text=input.nextLine();
if(text.matches("[^-](?!.*--)[A-Za-z0-9-]+[^-]"));{
System.out.println("Serial number "+text+" verification \nValid");
System.out.println("Enter a wildchar character: ");
cha=input.nextLine();
text= text.replaceAll("[A-Za-z0-9]", cha);
}
if(text.matches("[^A-Za-z0-9-]+"));
System.out.println("Only uppercase , lowercase letters, dashes and numbers are allowed.It should have exactly 2 non-consecutive dashes in the middle. ");
}
}
这是我到目前为止,但我不断收到这两个运行,而不是一个或另一个表达式的。一定有什么东西我失踪。提前致谢。
这里是我的准则:提示用户按照以下规则来输入序列号:
你的程序应该验证该序列号是有效的,如果是的话,提示用户输入通配符。那么就应该使用野生CHAR字符屏蔽所有的字符除了破折号显示隐藏的序列号
[什么代码应该看起来像]
[1]: https://i.stack.imgur.com/xoGke.jpg
点击此处摆脱分号...
if(text.matches("[^-](?!.*--)[A-Za-z0-9-]+[^-]"));{ << HERE
...and...
if(text.matches("[^A-Za-z0-9-]+")); << HERE
您块总是执行,因为if语句由于在该空块。
如果您在字符类有负则应该是第一:
沙到C0-yasht围巾是SH-A-TO-C0-yasht
但我不包括 - 在基团和写入图案为:[A-ZA-Z0-9] +( - [A-ZA-Z0-9] +){2}
或与POSIX:\ p {Alnum} +( - \ p {Alnum} +){2}
如果您要访问的3段:(\ p {} Alnum +) - (\ p {} Alnum) - (\ p {} Alnum)