用方法判断字符串数组中的元素长度是否为指定长度

package methodTest;

public class Method3 {
    //创建字符串数组保存JanCode
    //字符串长度是0,或大于0,小于3时,都不是完整的janCode。
    //只有长度等于3时才是完整的JanCode
    public static void main(String[] args) {
        String[] jan1 = {"AAA", "BBB", "CC"};
        String[] jan2 = {"AAA", "BBB"};
        String[] jan3 = {"AAA", "", "CCC","D"};
        String[] jan4 = {""};

        System.out.println("b1 = " + judgeLength(jan1));//false
        System.out.println("b2 = " + judgeLength(jan2));//true
        System.out.println("b3 = " + judgeLength(jan3));//false
        System.out.println("b4 = " + judgeLength(jan4));//false

    }

    private static boolean judgeLength(String[] strings) {
        boolean isFullJanCode = true;
        int count = 0;
        for (String jan : strings) {
            count++;
            if ((jan.length() > 0 && jan.length() < 3) || jan.length() == 0) {
                isFullJanCode = false;
                break;
            }
        }
        System.out.println();
        System.out.println("count = " + count);
        return isFullJanCode;
    }
}

在这里插入图片描述

package methodTest;

public class Method3 {
    //创建字符串数组保存JanCode
    //字符串长度是0,或大于0,小于3时,都不是完整的janCode。
    //只有长度等于3时才是完整的JanCode

    /**
     * 完全的なJanCodeの長さは13を表す。
     */
    public static final int FULL_JAN_LENGTH = 3;

    public static void main(String[] args) {
        String[] jan1 = {"AAA", "BBB", "CC"};
        String[] jan2 = {"AAA", "BBB"};
        String[] jan3 = {"AAA", "", "CCC", "D"};
        String[] jan4 = {"", "", ""};

        System.out.println("jan4 = " + jan4.length);
        System.out.println();

        System.out.println("isFullJanCode = " + judgeArrayLength(jan1));//false
        System.out.println("isFullJanCode = " + judgeArrayLength(jan2));//true
        System.out.println("isFullJanCode = " + judgeArrayLength(jan3));//false
        System.out.println("isFullJanCode = " + judgeArrayLength(jan4));//false

    }

    private static boolean judgeArrayLength(String[] strings) {
        //Janの桁数は13の場合、isFullJanCodeはtrue、仮にJanの桁数は13ではない。
        boolean isFullJanCode = false;
        for (String jan : strings) {
            if (!StringUtil.isNullString(jan)) {
                if (jan.length() == FULL_JAN_LENGTH) {
                    isFullJanCode = true;
                } else if (jan.length() > 0 && jan.length() < FULL_JAN_LENGTH) {
                    isFullJanCode = false;
                    break;
                }
            }
        }
        return isFullJanCode;
    }
}

class StringUtil {
    public static boolean isNullString(String target) {
        if (target != null && target.length() != 0) {
            return false;
        }
        return true;
    }
}

在这里插入图片描述