lundi 4 octobre 2021

Method keeps returning an if-statement even though the conditions are not meet

I spent a few hours on this assignment already but the gist of it is to write up a code that will ask a user for a username & password and 'create' an account for them. There are certain requirements for the username and password and they are as so:

Username must not start with a letter.
Username must contain at least 6 characters and no more than 12 characters.

Password must not contain the username.
Password must be at least 8 characters long and no more than 16 characters.

if (password.contains(username)) {
        System.out.println("password cannot contain the username");

It will return the other messages if the inputted password is <8 or >16 characters but this line always gets returned for some reason. I've input a password that contains no characters from the username and it will still return it. I'm in a bit of a pickle and require assistance. I don't understand what I am doing wrong either. In plain English, that line should do expect what I need it to: Check to see if password contains username!!

TIA

    import java.util.Scanner;

    public class Module2 {
    Scanner reader = new Scanner(System.in);
    String username;
    String password;

    public Module2() {
        this.username = "";
        this.password = "";

    }

    public static void main(String[] args) {

        System.out.println("starting application: initiating username and password creation");
        Module2 m2 = new Module2();
        m2.getUsername();


    }

    public String getUsername() {
        Scanner reader = new Scanner(System.in);
        System.out.println("new username: must start with a letter and be at least 6 characters 
    long but not more than 12 characters");
        //grabs input, store into String input
        String input = reader.next();
        char a = input.charAt(0);
        boolean v = Character.isLetter(a);
        //this entire statement will print the appropriate error messages
        if (!v) {
            System.out.println("username must begin with a letter");
        } else if (input.length() < 6) {
            System.out.println("username must be at least 6 characters long");
        } else if (input.length() > 12) {
            System.out.println("username must not be longer than 12 characters");
        } else {
            System.out.println("Thank you " + input);
            getPassword();
        }

        return "";
    }

    public String getPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("new password: must be at least 8 characters long but no more than 16 
    characters long and cannot contain the username");

        String password = scanner.nextLine();

        if (password.contains(username)) {
            System.out.println("password cannot contain the username");


        } else if (password.length() < 8) {
            System.out.println("password has to be at least 8 characters long");

        } else if (password.length() > 16) {
            System.out.println("password must not contain more than 16 characters");

        } else {
            System.out.println("Thank you\n" + password);
            System.out.println("username and password validated");
            System.out.println("ending application");
        }

        return "";
    }


}













Aucun commentaire:

Enregistrer un commentaire