如何检查数组是否不为空并且它的某些特定值是否为数字?

我有两个带有一些参数的声明数组。首先,我需要检查数组是否不为空,然后检查第一个数组的特定参数是否为数字。


现在我有两个数组。其中一个具有所有参数,第二个具有必须为数字的参数(也包含在第一个数组中)


我想在一个方法中进行这两项检查,因为我有两个不同的方法。


这就是我所拥有的


/** Array con parametros no obligatorios   Array con los parametros obligatorios. */

private static final String[] PARAMETROS_OBLIGATORIOS = new String[] {

    "idFichero","nombreFichero","qnuOrdest","idHsc","timCamestad","codGrupoest","qnuOrdestRcvd"

};


/** Array con los parametros que deben ser numericos. */

private static final String[] PARAMETROS_NUMERICOS = new String[] {

    "idFichero","idHsc","qnuOrdest","qnuOrdestRcvd"

};


private void validarObligatorios(final JobParameters parameters) throws JobParametersInvalidException {

    for (String nombre : PARAMETROS_OBLIGATORIOS) {

        if (StringUtils.isBlank(parameters.getString(nombre))) {

            String error ="El parametro " + nombre + " es obligatorio"; 

            LOGGER.error(error);

            throw new JobParametersInvalidException(error);

        }


    }

}




private void validarNumericos(final JobParameters parameters) throws JobParametersInvalidException {

    for (String nombre : PARAMETROS_NUMERICOS) {

        if (!StringUtils.isNumeric(parameters.getString(nombre))) {

            String error = "El parametro " + nombre + " debe ser numerico"; 

            LOGGER.error(error);

            throw new JobParametersInvalidException(error);

        }

    }

}

我该怎么做才能以独特的方法完成这两个步骤?


皈依舞
浏览 116回答 2
2回答

牛魔王的故事

替换您的 2 种方法validarObligatorios并validarNumericas通过以下单个功能回答您的问题吗?private void validarObligatoriosYNumericos(final JobParameters parameters) throws JobParametersInvalidException {    for (String nombre : PARAMETROS_OBLIGATORIOS) {        if (StringUtils.isBlank(parameters.getString(nombre))) {            String error ="El parametro " + nombre + " es obligatorio";             LOGGER.error(error);            throw new JobParametersInvalidException(error);        }    }    for (String nombre : PARAMETROS_NUMERICOS) {        if (!StringUtils.isNumeric(parameters.getString(nombre))) {            String error = "El parametro " + nombre + " debe ser numerico";             LOGGER.error(error);            throw new JobParametersInvalidException(error);        }    }}!

慕容708150

将数值列数组复制到列表中并用于contains查看是否应检查值。此解决方案假定数字列数组是强制列数组的子集,但看起来这是一个安全的假设。private void validarObligatorios(final JobParameters parameters) throws JobParametersInvalidException {&nbsp; &nbsp; List<String> numericColumns = Arrays.asList(PARAMETROS_NUMERICOS );&nbsp; &nbsp; String error = null;&nbsp; &nbsp; for (String nombre : PARAMETROS_OBLIGATORIOS) {&nbsp; &nbsp; &nbsp; &nbsp; if (StringUtils.isBlank(parameters.getString(nombre))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error ="El parametro " + nombre + " es obligatorio";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else if (numericColumns.contains(nombre) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!StringUtils.isNumeric(parameters.getString(nombre))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;error = "El parametro " + nombre + " debe ser numerico";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (error != null)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.error(error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new JobParametersInvalidException(error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java