A Java question about String.

I have an assignment about using String to write a program, but I’ve ever learn about how to use it. I hope somebody could help me figuring out this problem.

1. Contain at least seven characters.

2. Consist of at least one letter and one digit.

If two of it does match or not match, the system will return “valid password” or “invalid password”

1 Answer

Rating
  • 9 years ago
    Favorite Answer

    請參考我的做法

    import java.util.*;

    public class Y3714 {

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    while (true) {

    System.out.print("Input string ('exit' to quit): ");

    String s = scan.nextLine();

    if (s.equals("exit")) System.exit(0);

    // 1. verify if string contains at least 7 characters

    // 2. verify if string contains 1 letter and 1 digit at least

    if (s.length() < 7 || !s.matches("[a-z[A-Z]]+[0-9]+"))

    System.out.println("Invalid password");

    else

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

    }

    }

    }

Still have questions? Get your answers by asking now.