字符串的长度

时间:2012-11-17 19:07:13

标签: java methods if-statement invoke bluej

会员ID由一个独特的字母和数字序列*组成,长度最多为10个,并且引脚号由四个数字序列组成,我已经写了两个方法checkId和checkPassword来比较长度然后我称之为在构造函数中。但代码似乎不能正常工作

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");
        }
    }
}

1 个答案:

答案 0 :(得分:4)

变量password的类型为int。诸如此类的类型称为基元,因此它们没有诸如length()之类的方法。你可以改为Integer.toString(password).length()