Home>

If 7Caaad9 is input as input after executing the following logic,
I want to make Invalid3 output, but it doesn't work properly.

I want to do Invalid3 when three or more of the same character strings are consecutive.
The way to write line.matches (d) seems to be bad, it is always judged as true and becomes Invalid3.

import java.util. *;

public class Main {
public static void main (String [] args) {
Scanner sc = new Scanner (System.in);
String line = sc.nextLine ();
String a = "^ [0-9] + $";
String b = "^ [a-zA-Z] + $";
String c = "^ [0-9a-zA-Z] + $";
String d = "^ [0-9a-zA-Z] {3,} $";

if (line.length ()<6 || line.matches (a) || line.matches (b)) {
System.out.println ("Invalid");
return;
}
else if (! line.matches (c)) {
System.out.println ("Invalid2");
return;
}
else if (line.matches (d)) {
System.out.println ("Invalid3");
return;
}

System.out.println ("Valid");
}
}

  • Answer # 1

    Can I deny "(.) \ 1 {2}"?

  • Answer # 2

      

    [0-9a-zA-Z] {3,}

    I think this is an expression that says "3 or more letters or numbers".

    String d = ". * ([0-9a-zA-Z]) \\ 1 {2}. *";

    How is it?