简体   繁体   中英

length of a String

The member ID consists of a unique sequence of letters and digits* of length at most 10 and the pin number consists of a sequence of four digits, i have wrote tow methods checkId and checkPassword to compare the length and then i called them in the Constructor. but the code seems not working fine

public class Test
    {

    private String yourName;
    private String yourId;
    private int password;

    /**
     * Constructor for objects of class Test
     */
    public Test(String yourName, String yourId, int password)
    {
        // initialise instance variables
        this.yourName = yourName;
        this.yourId = yourId;
        this.password = password;
        checkPassword();
        checkId();
    }

    private void checkId()
    {
       int length; 
       length = yourId.length();
       if (length >= 10){
           System.out.print("the length must be less than 10, please change it");
        }
    }

    private void checkPassword()
    {
        int length;
        length = password.length();
        if (length != 4 ){
            System.out.println("must be at 4");
        }
    }
}

The variable password is of type int . Types such as these are called primitives, so they have no methods such as length() . You could do Integer.toString(password).length() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM